快速排序演算法
- 堆排序
- 快速排序
- 遞回
- 非遞回
- 歸并排序
- 遞回
- 非遞回
堆排序
時間復雜度:0(N*log(N))
空間復雜度:0(1)
穩定性:不穩定
private static void heapSort(int[] arr) {
//建堆
crearHeap(arr);
for (int i = 0; i < arr.length-1; i++) {
int heapSize=arr.length-i;
swap(arr,heapSize-1,0);
heapSize--;
shiftDown(arr,heapSize,0);
}
System.out.println(Arrays.toString(arr));
}
private static void crearHeap(int[] arr) {
// 從后往前遍歷(右邊非葉子節點開始), 依次進行向下調整
for (int i = (arr.length-1-1)/2; i >=0 ; i--) {
shiftDown(arr,arr.length,i);
}
}
//向下調整,形成大堆
private static void shiftDown(int[] arr, int size, int i) {
int parent = i;
int child = parent*2+1;
while (child<size){
if (child +1< size && arr[child +1]> arr[child]){
child=child+1;
}
if (arr[child]>arr[parent]){
swap(arr,child,parent);
}else {
break;
}
parent=child;
child=parent*2+1;
}
}
//交換
private static void swap(int[] arr, int child, int parent) {
int tmp =arr[child];
arr[child] =arr[parent];
arr[parent]=tmp;
}
快速排序
時間復雜度:O(N^ logN) 最壞的時候O(N^2) 和基準值密切相關
空間復雜度:0(logN) 最壞的時候O(N)
穩定性:不穩定
遞回
private static void quick(int[] arr) {
quickSortHelper(arr,0,arr.length-1);
System.out.println(Arrays.toString(arr));
}
private static void quickSortHelper(int[] arr, int left, int right) {
if (left>=right){
//區間只有一個元素,或者零個元素
return;
}
int index = partition(arr,left,right);
quickSortHelper(arr,left,index-1);
quickSortHelper(arr,index+1,right);
}
private static int partition(int[] arr, int left, int right) {
int i=left;
int j=right;
int baseValue=arr[right];
while (i<j){
while (i<j && arr[i]<=baseValue){
i++;
}
while (i<j && arr[j]>=baseValue){
j--;
}
if (i<j){
swap(arr,i,j);
}
}
swap(arr,i,right);
return i;
}
private static void swap(int[] arr, int i, int j) {
int tmp =arr[i];
arr[i]=arr[j];
arr[j]=tmp;
}
非遞回
public static void quickSortByLoop(int[] arr) {
Stack<Integer> stack =new Stack<>();
stack.push(0);
stack.push(arr.length-1);
while (!stack.isEmpty()){
int right = stack.pop();
int left = stack.pop();
if (left>=right){
continue;
}
int index = partition(arr,left,right);
//右子樹
stack.push(index+1);
stack.push(right);
//左子樹
stack.push(left);
stack.push(index-1);
}
System.out.println(Arrays.toString(arr));
}
private static int partition(int[] arr, int left, int right) {
int baseValue =arr[right];
int i =left;
int j =right;
while (i<j){
while (i<j && arr[i]<=baseValue){
i++;
}
while (i<j && arr[j]>=baseValue){
j--;
}
if (i<j){
swap(arr,i,j);
}
}
swap(arr,i,right);
return i;
}
private static void swap(int[] arr, int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
歸并排序
時間復雜度:O(NlogN)
空間復雜度:O(N) 如果是鏈表,可以為O(1)
穩定性:穩定
遞回
public static void mergeSort(int[] arr){
mergeSortHelper(arr,0,arr.length);
System.out.println(Arrays.toString(arr));
}
private static void mergeSortHelper(int[] arr, int left, int right) {
if (right-left<=1){
return;
}
int mid = (right+left)/2;
mergeSortHelper(arr,left,mid);
mergeSortHelper(arr,mid,right);
merge(arr,left,mid,right);
}
private static void merge(int[] arr, int left, int mid, int right) {
int cur1 =left;
int cur2 =mid;
//兩個陣列合并后的結果
int[] output=new int[right-left];
int outputIndex=0;
while (cur1<mid && cur2<right){
if (arr[cur1]<=arr[cur2]) {
output[outputIndex++] = arr[cur1++];
}else {
output[outputIndex++] = arr[cur2++];
}
}
while (cur1<mid){
output[outputIndex++] = arr[cur1++];
}
while (cur2<right){
output[outputIndex++] = arr[cur2++];
}
for (int i = 0; i < right-left ; i++) {
arr[left+i] = output[i];
}
}
非遞回
public static void mergeSortByLoop(int[] arr){
// gap 當前每個組中的元素個數.
for (int gap =1;gap<arr.length;gap*=2){
for (int i = 0; i <arr.length ; i+=2*gap) {
//相當于把兩個長度為 gap 的相鄰組進行了合并
int left =i;
int mid =i+gap;
int right=i+2*gap;
if (mid > arr.length){
mid =arr.length;
}
if (right>arr.length){
right=arr.length;
}
merge(arr,left,mid,right);
}
}
System.out.println(Arrays.toString(arr));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/397341.html
標籤:其他
