Quick Sort Example Program in Java


package in.malliktalksjava;

/**
* @author malliktalksjava
*
* Sort the given array using QuickSort Algorithm
*/
public class QuickSortExample {

private int array[];
private int length;

/**
* @param args
*
* Main Method. Calls the sort method and print the output into
* console.
*/
public static void main(String args[]) {
QuickSortExample quickSorter = new QuickSortExample();
int[] input = { 15, 42, 14, 100, 85, 1, 13, 6, 9 };
quickSorter.sortArray(input);
for (int count : input) {
System.out.print(count);
System.out.print(” “);
}
}

/**
* @param inputArr
*
* Checks given array is empty or null, if not calls quickSort
* method.
*/
public void sortArray(int[] inputArr) {

if (inputArr == null || inputArr.length == 0) {
return;
}
this.array = inputArr;
length = inputArr.length;
quickSort(0, length – 1);
}

/**
* @param lowerIndex
* @param higherIndex
*
* Sorts the given array
*/
private void quickSort(int lowerIndex, int higherIndex) {

int temp1 = lowerIndex;
int temp2 = higherIndex;
// calculate pivot number, here pivot is middle index number
int pivot = array[lowerIndex + (higherIndex – lowerIndex) / 2];
// Divide into two arrays
while (temp1 <= temp2) {
/**
* In each iteration, we will identify a number from left side which
* is greater then the pivot value, and also we will identify a
* number from right side which is less then the pivot value. Once
* the search is done, then we exchange both numbers.
*/
while (array[temp1] < pivot) {
temp1++;
}
while (array[temp2] > pivot) {
temp2–;
}
if (temp1 <= temp2) {
exchangeNumbers(temp1, temp2);
// move index to next position on both sides
temp1++;
temp2–;
}
}
// call quickSort() method recursively
if (lowerIndex < temp2)
quickSort(lowerIndex, temp2);
if (temp1 < higherIndex)
quickSort(temp1, higherIndex);
}

/**
* @param param1
* @param param2
*
* Exchange the numbers and set to instance array variable
*/
private void exchangeNumbers(int param1, int param2) {
int temp = array[param1];
array[param1] = array[param2];
array[param2] = temp;
}
}

 

Other Useful Links:

Bubble Sort Example in JAVA

Selection Sort Example Program in JAVA

Insertion Sort Example program in JAVA

Merge Sort Example in Java

Advertisements
About

I born in Good cultural place called India. My family is traditional family. We fallow all Indian traditions.

Tagged with: , , , , , , , , ,
Posted in JAVA
5 comments on “Quick Sort Example Program in Java
  1. […] Quick Sort Example Program in Java […]

  2. […] Quick Sort Example Program in Java […]

  3. B. M. says:

    I need to bookmark as I need a lot of java tutorials .. 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 678 other followers

Calendar
April 2014
M T W T F S S
« Feb   May »
 123456
78910111213
14151617181920
21222324252627
282930  
Mallikarjun Gunda

This is Mallikarjun Gunda, I am working as a software professional. Blogging is my Passion. This blog contains the posts on Java related technologies and programming languages. I am happy to share my knowledge through the blogs.

Blog Stats
  • 280,991 hits
IndiBlogger
malliktalksjava.wordpress.c...
70/100