冒泡排序是由两个for循环构成,第一个for循环的变量 i 表示总共需要多少轮比较,第二个for循环的变量 j 表示每轮参与比较的元素下标【0,1,……,length-i】,因为每轮比较都会出现一个最大值放在最右边,所以每轮比较后的元素个数都会少一个,这也是为什么 j 的范围是逐渐减小的。
1 2 3 4 5 6 7
voidbubble_sort(int arr[], int len){ int i, j; for (i = 0; i < len - 1; i++) for (j = 0; j < len - 1 - i; j++) if (arr[j] > arr[j + 1]) swap(arr[j], arr[j + 1]); }
归并排序
Now that we’ve understood this trick, here is my pseudocode of the merge sort.
1 2 3 4 5 6 7 8 9 10 11 12 13
arraymergeSort(array a) if(length(a)==1) return a[0]; end if //recursive calls [left_array right_array] := split_into_2_equally_sized_arrays(a); array new_left_array := mergeSort(left_array); array new_right_array := mergeSort(right_array); //merging the 2 small ordered arrays into a big one array result := merge(new_left_array,new_right_array); return result;
一直以来困惑于个数和长度的计算,比如 start = 71, end = 253,问你中间有多少个元素啊?
classSolution { intpartition(vector<int>& nums, int l, int r){ int pivot = nums[r]; int i = l - 1; for (int j = l; j <= r - 1; ++j) { if (nums[j] <= pivot) { i = i + 1; // 注意这里的顺序控制着下面swap要不要加一 swap(nums[i], nums[j]); } } swap(nums[i + 1], nums[r]); return i + 1; }
// 基于随机的划分 intrandomized_partition(vector<int>& nums, int l, int r){ int i = rand() % (r - l + 1) + l; swap(nums[r], nums[i]); return partition(nums, l, r); }
voidrandomized_selected(vector<int>& arr, int l, int r, int k){ if (l >= r) { return; } int pos = randomized_partition(arr, l, r); int num = pos - l + 1; if (k == num) { return; } elseif (k < num) { randomized_selected(arr, l, pos - 1, k); } else { randomized_selected(arr, pos + 1, r, k - num); } }
public: vector<int> getLeastNumbers(vector<int>& arr, int k){ srand((unsigned)time(NULL)); randomized_selected(arr, 0, (int)arr.size() - 1, k); vector<int> vec; for (int i = 0; i < k; ++i) { vec.push_back(arr[i]); } return vec; } };
intpartition(vector<int> &vi, int low, int up) { int pivot = vi[up]; int i = low-1; for (int j = low; j < up; j++) { if(vi[j] <= pivot) { i++; swap(vi[i], vi[j]); } } swap(vi[i+1], vi[up]); return i+1; } //C++'s array range should be [low, up], the same as [low, up+1) voidquickSort(vector<int> &vi, int low, int up) { if(low < up) { int mid = partition(vi, low, up); //Watch out! The mid position is on the place, so we don't need to consider it again. //That's why below is mid-1, not mid! Otherwise it will occur overflow error!!! quickSort(vi, low, mid-1); quickSort(vi, mid+1, up); } }
voidQuickSort(int *a, int left,int right) { if (a == NULL || left < 0 || right <= 0 || left>right) return; stack<int>temp; int i, j; //(注意保存顺序)先将初始状态的左右指针压栈 temp.push(right);//先存右指针 temp.push(left);//再存左指针 while (!temp.empty()) { i = temp.top();//先弹出左指针 temp.pop(); j = temp.top();//再弹出右指针 temp.pop(); if (i < j) { int k = Pritation(a, i, j); if (k > i) { temp.push(k - 1);//保存中间变量 temp.push(i); //保存中间变量 } if (j > k) { temp.push(j); temp.push(k + 1); } }
classSolution { public: voidmaxHeapify(vector<int>& a, int i, int heapSize){ int l = i * 2 + 1, r = i * 2 + 2, largest = i; if (l < heapSize && a[l] > a[largest]) { largest = l; } if (r < heapSize && a[r] > a[largest]) { largest = r; } if (largest != i) { swap(a[i], a[largest]); maxHeapify(a, largest, heapSize); } }
voidbuildMaxHeap(vector<int>& a, int heapSize){ for (int i = heapSize / 2 - 1; i >= 0; --i) { maxHeapify(a, i, heapSize); } }
intfindKthLargest(vector<int>& nums, int k){ int heapSize = nums.size(); buildMaxHeap(nums, heapSize); for (int i = nums.size() - 1; i >= nums.size() - k + 1; --i) { swap(nums[0], nums[i]); --heapSize; maxHeapify(nums, 0, heapSize); } return nums[0]; } };