Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

Quicksort in Java

Lars Vogel

Version 0.6

06.08.2010

Revision History
Revision 0.117.05.2009Lars Vogel
Created
Revision 0.2 - 0.6 06.08.2010Lars Vogel
bug fixes and enhancements

Quicksort with Java

This article describes how to implement Quicksort with Java.


Table of Contents

1. Quicksort
1.1. Overview
1.2. Description of the algorithm
2. Quicksort in Java
2.1. Implementation
2.2. Test
3. Complexity Analysis
4. Thank you
5. Questions and Discussion
6. Links and Literature
6.1. Source Code
6.2. General

1. Quicksort

1.1. Overview

Sort algorithms are ordering the elements of an array according to a predefined order. Quicksort is a divide and conquer algorithm. In a divide and conquer sorting algorithm the the original data is separated into two parts (divide) which are individually sorted (conquered) and then combined.

1.2. Description of the algorithm

If the array contains only one element or zero elements then the array is sorted.

If the array contains more then one element then:

  • Select an element from the array. This element is called the "pivot element". For example select the element in the middle of the array.

  • All elements which are smaller then the pivot element are placed in one array and all elements which are larger are placed in another array.

  • Sort both arrays by recursively applying Quicksort to them.

  • Combine the arrays

Quicksort can be implemented to sort "in-place". This means that the sorting takes place in the array and that no additional array need to be created.