sort algorithms
//todo
#include<iostream> void swap(int *a, int *b){int temp = *a; *a = *b; *b = temp;} void print_array(int *arr, int len) { for (int i = 0; i < len; i++) std::cout << arr[i] << " , "; } // swap sort (bubble sort), bubble the largest item to top of list, impractical void bubble_sort(int arr[], int len) { bool need_sort = true; while (need_sort) { need_sort = false; for (int i = 0; i < len - 1; i++) { if (arr[i] < arr[i + i]){swap(arr+i, arr+i + 1);need_sort = true;} } } } // selection sort, select the largest one, and put it in the top, just need O(n) insert operation!!! void select_sort(int arr[], int len) { for (int i = 0; i < len - 1; i++) { int index = i; for (int j = i + 1; j < len; j++) { if (arr[j] > arr[index]) index = j; } swap(arr+i, arr+index); } } // the only advantage of both bubble sort and selection sort is easy to implement, should not use it // insert sort, choose item in list one and insert into another in right order // efficient for small lists and mostly-sorted list void insert_sort(int arr[], int len) { for (int i = 0; i < len-1; i++) { int temp = arr[i + 1]; int j = i; for (; j >= 0 && (arr[j] < temp); j--) { arr[j+1] =arr[j]; } if (j < i) arr[j+1] = temp; } } // as the result list is sorted, insert one new item is same to : put the new item in the top, // bubble (swap) it to the right position (just need one-time bubbling through the list) void insert_sort2(int arr[], int len) { for (int i = 1; i < len; i++) { for (int j = i - 1; j >= 0 && (arr[j] < arr[j + 1]); j--) { swap(arr + j, arr + j + 1); } } } // shell sort, combine insert sort and group (because insert sort is efficient when the list is mostly-sorted, // one of the fastest algorithms for small number of elements( < 1000 ), and it needs little momery // unlike efficient sorting algorithms, Shellsort does not require use of the call stack, making it useful in embedded systems where memory is at a premium void shell_sort(int arr[], int len) { for (int gap = len / 2; gap >0; gap /= 2) { for (int i = 0; i < gap; i++) { for (int k = i + gap; k < len; k += gap) { for (int j = k - gap; j >= 0 && (arr[j] < arr[j + gap]); j -= gap) swap(arr + j, arr + j + gap); } } } } // merge sort, recursion and double memory of data void merge_sorted_array(int a[], int lena, int b[], int lenb, int c[]) { int i(0), j(0), m(0); while (i < lena && j << lenb) { if (a[i] < b[j]) c[m++] = a[i++]; else c[m++] = b[j++]; } while (i < lena) c[m++] = a[i++]; while (j < lenb) c[m++] = b[j++]; } void merge_array(int a[], int first,int mid, int last, int temp[]) { int i(first), j(mid), m(0); while (i < mid && j <=last) { if (a[i] < a[j]) temp[m++] = a[i++]; else temp[m++] = a[j++]; } while (i < mid) temp[m++] = a[i++]; while (j <=last) temp[m++] = a[j++]; for (i = first, m = 0; i <= last;) a[i++] = temp[m++]; } void merge_sort(int a[], int first_index, int last_index, int temp[]) { if (last_index>first_index) { int mid = (first_index + last_index) / 2; merge_sort(a, first_index, mid, temp); merge_sort(a, mid + 1, last_index, temp); merge_array(a, first_index, mid+1, last_index, temp); } } // quick sort, choose a pivot, put all elements smaller before it! // Typically, quicksort is significantly faster in practice than other Θ(nlogn) algorithms // in-place, need less swap than merge-sort void quick_sort(int a[], int first, int last) { if (!(first < last)) return; int i = first, j = last, temp = a[i]; while (i < j) { while (i < j && temp < a[j]) j--; if (i < j) a[i++] = a[j]; while (i < j && a[i] < temp) i++; if (i < j) a[j--] = a[i]; } a[i] = temp; quick_sort(a, first, i - 1); quick_sort(a, i + 1, last); } int main() { int array01[] = { 1, 42, 2, 7, 3, 4, 9, 33, 6 }; int temparr[9] = { 0 }; quick_sort(array01, 0,8); print_array(array01, 9); }