10. Rewrite the sorting algorithms BubbleSort(), SelectionSort() and InsertionSo
ID: 3919422 • Letter: 1
Question
10. Rewrite the sorting algorithms BubbleSort(), SelectionSort() and InsertionSort() (found on the class webpage in Examples/Lecture/C_Programs/SortingSearching/Sort.c) so that they sort in decreasing instead of increasing order.
Explanation / Answer
void BubbleSort(int* A, int n){ int i, j; for(j=n-1; j>0; j--){ for(i=1; iA[i-1] ){ swap(A, i, i-1); } } } } void SelectionSort(int* A, int n){ int i, j, imax; for(j=n-1; j>0; j--){ imax = 0; for(i=1; iA[i] ){ imax = i; } } swap(A, imax, j); } } void InsertionSort(int* A, int n){ int i, j, temp; for(j=1; j=0 && temp>A[i] ){ A[i+1] = A[i]; i--; } A[i+1] = temp; } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.