Program 4
ASCENDING SORT
The function/method ascendingSort performs an in-place sort on the given input fist which will be sorted in ascending order.
The function/method ascendingSort accepts two arguments – len
, an integer representing the length of the input list and arr, a list of integers representing the input list, respectively
The function/method ascendingSort compiles successfully but fails to get the desired result for some test cases due to logical errors. Your task is to fix the code so that it passes all the test cases.
Incorrect Code
void ascendingSort (int arr[], int size) { int i, j; for (i = 0; i < size - 1; i++) { for (j = i; j < size; j++) { if (arr[i] < arr[j]) { // Swap arr[j] and arr[j + 1] int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } }
Correct Code
void ascendingSort (int arr[], int size) { int i, j; for (i = 0; i < size - 1; i++) { for (j = i; j < size; j++) { if (arr[i] > arr[j]) { // Swap arr[j] and arr[j + 1] int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } }
Login/Signup to comment