...

/

Solution Review: Sort an Array

Solution Review: Sort an Array

This lesson will give a detailed solution review for the problem of the previous lesson.

Solution: Sorting the Array

Press + to interact
class Solution {
public static void sortArray(int[] array, int n) {
if (n==1) {
return;
}
for (int i = 0; i < n-1; i++) {
if (array[i] > array[i+1]) {
int temp = array[i];
array[i] = array[i+1];
array[i+1] = temp;
}
}
sortArray(array, n-1);
}
public static void main(String[] args) {
System.out.println("Unsorted Array: ");
int[] array = {40, 24, 60, 15, 10, 45, 93};
//int[] array = {1,2,2,2,1};
System.out.print("{ ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println("} ");
System.out.println("Sorted Array: ");
sortArray(array, array.length);
System.out.print("{ ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println("} ");
}
}

Understanding the code

The recursive code can be broken down into two parts: the recursive method and the main where the method is called.

Driver Method

The driver code is found from line 18 to line 37.

  • An array is defined in the driver code.
  • The code from line 23 to line 27 prints the unsorted array.
...