class Hw7_2
{
public static void main(String[] args)
{
int[] array = {56, 23, 78, 91, 2, 45, 11, 37, 10, 27, 82, 13, 90}; //initialize unsorted array
int[] sorted = new int[array.length]; //create empty array for sorted array
printArray(array); //print unsorted array
sort(array,0,array.length-1); //call sorting method
for(int i=0;i<array.length;i++) //loop to input sorted array
sorted=array;
printArray(sorted); //print sorted array
}
public static void sort(int[] a, int s, int e) //sort method
{
if(s<e) //termination condition, end if start index is == to end index
{
int index = partition(a,s,e); //get index from partition method
if (index>1) //condition to check if subarray is greater than 1
sort(a,s,index-1); //recursive call incrementing end index down
if (index+1<e) //check if start of subaray is less than end of array
sort(a,index+1,e); //recursive call incrementing starting index up
}
}
public static int partition(int[] a, int s, int e) //partition method
{
int indexNum = a; //set placeholder for index number
while(0<1) //infinite loop until return condition met
{
while(a<indexNum) //increment starting index up if start less than index number
s++;
while(a[e]>indexNum) //increment ending index down if start less than index number
e--;
if(s<e) //condition to swap number
{
int temp=a[e]; //placeholder for swapping places of numbers
a[e]=a; //swap last in subarray with first in subarray
a=temp; //swap first in sub array with placeholder number
}
else
{
return e; //terminate loop and return ending index of subarray
}
}
}
public static void printArray(int[] a) //print method
{
for(int i=0;i<a.length;i++)
System.out.print(a+" ");
System.out.println();
}
}