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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

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

Join 680 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
  • 222,930 hits
IndiBlogger
malliktalksjava.wordpress.c...
70/100
All tags
Apache Cassandra Arrays in Java Cassandra Cassandra Database Cassandra examples client stubs creating a java web service creating a web service using java difference between interface and abstract class Differences between SOAP and RESTfull webservices Display div as Overlay using JQuery download the spring basic application File directory Generating webservice client stubs Hibernate hibernate exceptions How to display over using JQuery how to generated web service client stubs in STS How to implement Spring basic application how to write singleton design pattern in java interface in java Java Java Arrays Java Example Programs Java examples Java interview questions java programs java project Java sample Programs JavaScript Java Script JavaServer Faces Java Sorting alogorithms Java Tutorials javatutorials Java Web services JDBC open shift openshift Overlay implementation using JQuery Overlay in JQuery POI API POI Examples public class public static void main Redhat OpenShift representational state transfer Restfull webservices Sample Overlay html with JQuery sample programs in java Service-oriented architecture Singleton design pattern in java SOAP SOAP Webservices software Spring Framework Spring Interview Questions Spring MVC Spring MVC Example program Spring MVC sample Program Spring Sample Programs Spring Tutorials Struts technology Top Java Interview questions w3c dom Web service web service client Web Service Description Language Web services webservices writing singleton design pattern in Java WSDL Xml xml parsers