Quick Sort Example-java

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.