快速排序(不确定是否有问题)
快速排序的简单代码
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
#define MAX_NUM 1000
int arr[MAX_NUM];
void generate(){
srand(time(0));
for(int i = 0; i < MAX_NUM; i++) {
arr[i] = 1000 - i;
}
}
void print() {
for(int i = 0; i < MAX_NUM; i++) {
cout << arr[i] << " ";
if(i % 15 == 0) {
cout << " " << endl;
}
}
}
void quicksort(int* arr, int start , int end) {
if(start >= end) {
return;
}
int s = arr[start];
int i = start;
int j = end - 1;
while(i < j) {
while(s >= arr[i]){
i++;
}
while(s < arr[j]){
j--;
}
if(i <= j){
swap(arr[i] , arr[j]);
}
}
swap(arr[start], arr[j]);
quicksort(arr, start, j);
quicksort(arr, j+1, end);
}
int main(){
generate();
quicksort(arr, 0 , MAX_NUM);
print();
return 0;
}