Skip to main content

Quick sort Implementation

 Quick sort : In quick sort, we will consider a pivot element and divide the array so that all the lesser element will be in left side of pivot element while all the greater element will be in right side.

   This process we will continue recursively until array is fully sorted.
     Time complexity will beO(nlogn) but in worst case when elements are in sorted order time complexity will become O(n^2) as in each call only one element will be consider as sorted and for rest again we need to follow same process.
     space complexity will be O(1).

class QuickSort {

public static void sort(int[] array) {
int n = array.length;
shuffle(array);
sort(array, 0, n - 1);
}

private static void sort(int[] array, int lo, int hi) {
if (hi <= lo) return;
int j = partition(array, lo, hi);
sort(array, lo, j - 1);
sort(array, j + 1, hi);
}

private static int partition(int[] array, int lo, int hi) {
int i = lo + 1;
int j = hi;
int pivot = array[lo];
while (i <= j) {
while (array[i] <= pivot) {
i++;
if (i > hi) break;
}
while (array[j] >= pivot) {
j--;
if (j == lo) break;
}
if (i >= j) break;
swap(array, i, j);
}
swap(array, lo, j);
return j;
}

private static void shuffle(int[] array) {
Random random = new Random();
for (int i = 1; i < array.length; i++) {
int j = random.nextInt(i + 1); // [0, i]
swap(array, i, j);
}
}

private static void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}

private static void print(int[] array) {
System.out.print("[");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
if (i < array.length - 1) {
System.out.print(", ");
}
}
System.out.println("]");
}

public static void main(String[] args) {
int[] A = {111, 80, 14, 21, 19, 31, 19, 121, 10, 190, 80, 6, 7};
sort(A);
print(A);
}
}

Please have a look to the  source code of all sorting method from below link
  1. BUBBLE SORT PROGRAM
  2. SELECTION SORT PROGRAM
  3. INSERTION SORT PROGRAM
  4. MERGE SORT PROGRAM
  5. QUICK SORT PROGRAM

Comments

Popular posts from this blog

Bubble sort Implementation

Bubble sort  : In bubble sort ,we will select the 1st element and compare with all the remaining element, same process we will continue for all the elements as we are traveling the whole Array 2 times except the element which we have selected to compare with other elements but still it will be consider as n time.    So time complexity for bubble sort will be O(n^2).         space complexity for bubble sort will be O(1). // Bubble Sort class BubbleSort { public static void sort ( int [] array) { int n = array. length ; while ( true ) { boolean swapped = false; for ( int i = 0 ; i < n - 1 ; i++) { if (array[i + 1 ] < array[i]) { swap (array , i , i + 1 ) ; swapped = true; } } if (!swapped) break; } } private static void swap ( int [] array , int i , int j) { int temp = array[i] ;...

Object-Oriented Programming Concept in Java

OOPS( Object-Oriented Programming ) Concept in Java :   As we all know Java is Object Oriented programming language and what exactly it means in simple words to understand can be described as whatever is going to happen by Java , it will be based on some Object.  So next question can be what is Object ? , "Object is the representation or reference of Class to access its properties and use its behaviour ", now next is What is Class in java and answer to this question is "A class in java is the blueprint of Properties and Behaviours of it's own Object" as explained in my previous post  BASIC OVERVIEW OF JAVA  (SESSION 1)   Let's understand through an example : public class FirstJavaProgram { int firstNumber=10; int secondNumber=20;      public int sum(int fNum, int sNum){         return fNum+sNum;     }     public static void main(String[] args) {     //our logics ...

Overview of time and space complexity for sorting algorithm

Bubble sort : In bubble sort ,we will select the 1st element and compare with all the remaining element, same process we will continue for all the elements as we are traveling the whole Array 2 times except the element which we have selected to compare with other elements but still it will be consider as n time.    So time complexity for bubble sort will be O(n^2).         space complexity for bubble sort will be O(1). Selection sort : In selection sort we will divide the Array in 2 parts , sorted and unsorted and select the min element from unsorted and will copy to sorted array.      Time Complexity for selection sort also is O(n^2).      space complexity for bubble sort will be O(1). Insertion sort :  In insertion sort, Same process like selection sort , we will divide the array in sorted and unsorted array and we will select element from unsorted array and will insert it into sorted array at its proper...