0
作業分配的一部分是在'函數式'中用C++編寫快速排序。我的代碼大部分都被編譯了,但是我遇到的唯一問題是對數組的更改不能保留。這裏是我的代碼,我知道這可能無法完全像函數式編程,但我的老師說,這是罰款:未對數組進行更改
#include <stdio.h>
#include <iostream>
void swap(int* x, int* y);
int partition(int arr[], int low, int high, int cursor);
void quickSort(int A[], int start, int end);
int main(){
int list[20] = {20, 5, 12, 11, 1, 6, 18, 7, 2, 3, 14, 19, 10, 4, 13, 8, 15, 9, 17, 16};
quickSort(list,0,19);
for(int count =0; count<20; count++){
std::cout<< list[count] << " ";
}
};
void swap(int* x, int* y){
int temp = *x;
*x = *y;
*y = temp;
};
int partition(int arr[], int low, int high, int cursor){
if(low == high){
swap(&arr[cursor+1], &arr[high]);
return cursor+1;
}else if(arr[low] <= arr[high]){
swap(&arr[cursor+1], &arr[low]);
return partition(arr, low+1, high, cursor+1);
}else if(arr[low] > arr[high] && low != high){
return partition(arr, low+1, high, cursor);
}
};
void quickSort(int A[], int start, int end){
if(start < end){
int p = partition(A, start, end, start-1);
quickSort(A, start, p-1);
quickSort(A, p+1, end);
}
};
''low!= high && arr [low]> arr時,partition()不會返回任何內容[高]'。先修復它。 – timrau
我使用這裏的代碼作爲參考,它似乎沒有使用這種情況:http://www.geeksforgeeks.org/iterative-quick-sort/ –
作出了更改。 –