Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Write the code of Quick Sort in JAVA that sort an array.

The array initialized through shorthand


format and print table containing following information low, high, pi, sort_condition, pivot, i, j,
p_condition, swap1, swap2 and current_array.

Required Things:
I. Code in text form
II. Screen Short of Code
III. Screen Short of Output [Sorted Array and Table]

The Table must contain following information:


1) low = Minimum index
2) high = Maximum index
3) pi = return value from Partition function.
4) sort_condition = Condition in Sort Function (low < high)
5) pivot = Pick last element as a pivot.
6) i = index of smaller element
7) j = Loop Variable
8) p_condition = Partition Condition (arr[j] < Pivot)
9) swap1 = swap inside Partition-if-body through i & j
10) swap2 = swap for pivot in Partition function, after if body
11) current_array = like 10,80,30,90,40,50,70

Sample of Table

Due Time: 4:30 PM, Dec 10, 2020.


Best of Luck!

The End
SOLUTION
FA19-BCS-061

CODE in TEXT FORM:

package quick_sort_s2;
import java.util.Arrays;

public class Quick_Sort_s2 {

int j;
int partition(int arr[], int Low, int High){ ////partition part
int pivot = arr[High];
int i = (Low-1);
for(int j=Low; j<High; j++) /////for loop starting
{

Page 1 of 5
if (arr[j]<pivot) ////condition check
{ System.out.println(Low+"\t"+High+"\t"+(i+2)+"\t"+Low+"<"+High+"\t \t"+pivot+"\t"+
(i+1)+"\t"+j+"\t"+"arr["+j+"]"+"<"+pivot+"\t"+arr[i+1]+","+arr[j]
+"\t"+"N/A"+"\t"+Arrays.toString(arr));

i++;
int a= arr[i];
arr[i]=arr[j]; ////swaping occuring here
arr[j]= a;
}

}///for loop end

int a= arr[i+1];
arr[i+1]=arr[High];
arr[High]= a; //////////sout to show after if values in table
System.out.println(Low+"\t"+High+"\t"+(i+1)+"\t"+Low+"<"+High+"\t \t"+pivot+"\t"+(i)
+"\t"+j+"\t"+"arr["+j+"]"+"<"+pivot+"\t"+"N/A"+"\t"+arr[i]+","+arr[High]
+"\t"+Arrays.toString(arr));

return i+1;

void sort(int arr[], int Low, int High){ ///sort function


int pivot = arr[High];
int i = (Low-1);
if(Low<High){
int pi=partition(arr, Low, High);
for(int j=Low; j<High; j++)
FA19-BCS-061
{
System.out.println(Low+"\t"+High+"\t"+pi+"\t"+Low+"<"+High+"\t \t"+pivot+"\t"+
(i+1)+"\t"+j+"\tarr["+j+"]<"+pivot+"\t"+arr[i+1]+","+arr[j]+"\t"+"N/A"+"\t"+Arrays.toString(arr));
}
sort(arr,Low,pi-1);
sort(arr,pi+1,High);

}
}
static void SortedArray(int arr[]){

for(int i=0;i<arr.length;++i){ //for start


System.out.print(+arr[i]+" ");} //for end
} //function end

Page 2 of 5
public static void main(String[] args) {
System.out.println("\t\t\t\t\t\tQuick Sortion Algorithme \n");
System.out.println("Low" + "\tHigh"+"\tpi"+"\tSort_Condition"
+"\tPivot"+"\ti"+"\tj"+"\tP_Condition"+"\tSwap_1"+"\tSwap_2"+"\tCurrent_Array");

int arr[]={10,92,61,50,80,30,42};

int no= arr.length;


Quick_Sort_s2 f_call=new Quick_Sort_s2();

f_call.sort(arr,0,no-1);
System.out.println("\n <<<<<<<<<<<<<<Sorted Array>>>>>>>>>>>>>");
System.out.print("{ ");
SortedArray(arr);
System.out.print("} \n");

FA19-BCS-061
SCREENSHOT of CODE

Page 3 of 5
FA19-BCS-061

Page 4 of 5
SCREENSHOT OF OUTPUT:

Page 5 of 5

You might also like