我从我的文本编辑器复制到博客上,请问一下这算不算copy?
#include<iostream>
using namespace std;
// two kind of partions
// partion one: from CLRS
// use the last element a[r] as pivot
int partition(int a[], int p, int r){
// i will be the pivot's place
int i = p - 1;
for(int j = p; j < r; j++){
// put the elements less than pivot before the index i
if(a[j] < a[r]){
swap(a[++i], a[j]);
}
}
swap(a[++i], a[r]); // std:swap in C++ 11, in <algorithm> for previou sversion
return i;
}
// // ! partion method two: from other books, like Cracking the code intervew 5th
// int partition(int a[], int p, int r){
// int pivot = a[(p + r) / 2];
// while(p <= r){
// // find elments should be on the left
// while(a[p] < pivot) p++;
// // find elements should be on the right
// while(a[r] > pivot) r--;
// if(p <= r){
// swap(a[p], a[r]);
// p++;
// r--;
// }
// }
// return p;
// }
void quicksort(int a[], int p, int r){
if(p < r){
int q = partition(a, p, r);
quicksort(a, p, q - 1); // left part
quicksort(a, q + 1, r); // right part
}
}
int main(){
int arr[] = {2, 45, 5, 7, 34, 456, 345, 89, 8, 1, 341, 4, 98, 67};
int length = sizeof(arr) / sizeof(arr[0]);
quicksort(arr, 0, length);
for(int i = 0; i < 14; i++){
cout << arr[i] << " ";
}
cout << endl;
}
快速排序 算法导论
最新推荐文章于 2024-08-04 23:11:18 发布