Search⌘ K
AI Features

Solution Review: Sort an Array

Explore the step-by-step process of sorting an array through recursion in Java. Understand the driver method, base case, recursive case, and how the stack manages recursive calls to achieve array sorting.

Solution: Sorting the Array

Java
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 ...