Package Quicksort;
/**
* Quick Sort
* @author Administrator
*
*/
public class Testmain
{
/**
*
* @param start position
* @param End Position
*/
public static void Sortarray (int[] array,int start,int end)
{
if (Start >= end)
{
return;
}
int startIndex = start;
int endIndex = end;
while (Startindex<endindex)
{
/** from Endindex forward until it finds a smaller element than the current one, and then swaps the position **/
while (Startindex<endindex && Array[startindex]<=array[endindex])
{
endindex--;
}
Exchange (ARRAY,STARTINDEX,ENDINDEX);
/** from Endindex backward until it finds a larger element than the current one, and then swaps the position **/
while (Startindex<endindex && Array[startindex]<=array[endindex])
{
endindex--;
}
Exchange (ARRAY,STARTINDEX,ENDINDEX);
}
Recursive previous array
Sortarray (array,start,startindex-1);
Recursive after an array
Sortarray (Array,startindex+1,end);
}
/****/
public static void Exchange (int[] Array,int source,int target)
{
int temp = Array[source];
Array[source] = Array[target];
Array[target] = temp;
}
public static void Main (string[] args)
{
Int[] Testarray = {19,4,2,10,18,29,14,14,13,12,18,17};
Sortarray (testarray,0,testarray.length-1);
for (int i:testarray)
{
System.out.print (i+ ",");
}
}
}
Quick Sort Example-java