Sat Oct 02 2021

Quick Sort

File Name: quick-sort.java

import java.io.*;

class sort {
	int list[];

	/* Constructor of the class */
	sort(int ... array) {
		list = new int[array.length-1];
		list = array;
		quick(0,array.length-1);
		display();
	}
										
	void quick(int start, int end) {
		int i = start;
		int j = end;
		int pivot = list[(start + end) /2];
		if(i <= j) {
			while(list[i] < pivot)
				i++;
			while(pivot < list[j])
				j--;
		}
		if(i <= j) {
			int temp = list[i];
			list[i] = list[j];
			list[j] = temp;
			i++;
			j--;
		}
		if(start < j)
			quick(start,j);
		if(i < end)
			quick(i,end);
	}
										
	void display() {
		System.out.println("Sorted array:");
		for(int k:list)
			System.out.println(k);
	}
}
										
class quicksort {
	public static void main(String args[ ]) {
		new sort(6,2,4,1,3,0,5,8,7,9);
	}
}




/* Output */
Sorted array:
0
1
2
3
4
5
6
7
8
9
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.