面试题2:自己实现七个排序算法
Posted wxquare的学习笔记
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了面试题2:自己实现七个排序算法相关的知识,希望对你有一定的参考价值。
冒泡排序:
template<typename Dtype> void bubbleSort(Dtype arr[], int len) { if (len < 2) return; bool isSorted; for (int i = 0; i < len - 1; i++) { isSorted = true; for (int j = 0; j < len - 1; j++) { if (arr[j] > arr[j + 1]) { isSorted = false; std::swap(arr[j], arr[j + 1]); } } if(isSorted) return; } }
选择排序:
void selectSort(vector<int>& arr) { int n = arr.size(); if (n <= 1) return; for (int i = 0; i < n-1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) minIndex = j; } if(minIndex != i) swap(arr[i],arr[minIndex]); } }
插入排序:
//插入排序 void insertSort(int* arr, int len) { if (len < 2) return; int temp,j; for (int i = 1; i < len; i++) { if (arr[i] < arr[i - 1]) { temp =arr[i]; for (j = i - 1; j >= 0 && arr[j] > temp; j--) { arr[j + 1] = arr[j]; } arr[j+1] = temp; } } }
快速排序:
template<typename Dtype> void quickSort(Dtype arr[], int left, int right) { if (left >= right) return; Dtype pivotVal = arr[left]; int l = left; int r = right; while(l < r){ while(l < r && arr[r] >= pivotVal) r--; arr[l] = arr[r]; while(l < r && arr[l] <= pivotVal) l++; arr[r] =arr[l]; } arr[l] = pivotVal; quickSort(arr, left, l- 1); quickSort(arr, l + 1, right); } template<typename Dtype> void quickSort(Dtype arr[], int len) { if (len < 2) return; quickSort<Dtype>(arr, 0, len - 1); }
归并排序:
class MergeSort { private: void mergeSort(vector<int>&arr, int left, int right) { if (left >= right) return; int mid = (left + right) / 2; mergeSort(arr, left, mid); mergeSort(arr, mid + 1, right); merge(arr, left, mid, right); } void merge(vector<int>&arr, int left, int mid, int right) { int* temp = new int[right - left + 1]; int i = left; int j = mid + 1; int k = 0; while (i <= mid && j <= right) { if (arr[i] <= arr[j]) temp[k++] = arr[i++]; else temp[k++] = arr[j++]; } while (i <= mid) temp[k++] = arr[i++]; while (j <= right) temp[k++] = arr[j++]; int n = right - left + 1; for (int i = 0; i < n; i++) { arr[left + i] = temp[i]; } delete[] temp; } public: void mergeSort(vector<int>& arr) { int n = arr.size(); if (n <= 1) return; mergeSort(arr, 0, n - 1); } };
堆排序:
class HeapSort { private: void heapAdjust(vector<int>&arry, int start, int size) { int lchild = 2 * start + 1, rchild = lchild + 1; while (rchild < size) { if (arry[start] >= arry[lchild] && arry[start] >= arry[rchild]) return; if (arry[lchild] >= arry[rchild]) { swap(arry[start], arry[lchild]); start = lchild; } else { swap(arry[start], arry[rchild]); start = rchild; } lchild = start * 2 + 1; rchild = lchild + 1; //重新计算子树位置 } if (lchild < size && arry[lchild] > arry[start]) //只有左子树且子树小于自己 { swap(arry[lchild], arry[start]); } } public: void heapSort(vector<int>& arr) { int n = arr.size(); if (n <= 1) return; //建立大顶堆 for (int i = n / 2 - 1; i >= 0; i--) { heapAdjust(arr, i, n); } //依次选择最大的数 for (int i = n - 1; i >= 0; i--) { swap(arr[0], arr[i]); heapAdjust(arr, 0, i - 1); } } };
希尔排序:
//shell排序 void shellSort(int* arr, int len) { int gap = len; while (gap > 1) { gap = (gap + 1) / 2; for (int i = 0; i < len - gap; i++) { if (arr[i + gap] < arr[i]) { swap(arr[i + gap], arr[i]); } } } }
以上是关于面试题2:自己实现七个排序算法的主要内容,如果未能解决你的问题,请参考以下文章