Lapping data structures and algorithms-09 Quick Sort

Source: Internet
Author: User

Quick sort:

/*

* Quick Sort

*/

public class QuickSort {


/**

* Divide arrays

*/

public static int partition (long arr[],int left, int. Right,long point) {

int leftptr = left-1;

int rightptr = right;

while (true) {

Loop, will be smaller than the keyword left in the left side

while (Leftptr < rightptr && Arr[++leftptr] < point);

Loop, will be larger than the keyword left on the right side

while (Rightptr > Leftptr && arr[--rightptr] > point);

if (leftptr >= rightptr) {

Break

} else {

Long tmp = arr[leftptr];

ARR[LEFTPTR] = arr[rightptr];

ARR[RIGHTPTR] = tmp;

}

}

Swap the keyword with this one referred to by the current leftptr

Long tmp = arr[leftptr];

ARR[LEFTPTR] = Arr[right];

Arr[right] = tmp;

return leftptr;

}

public static void Displayarr (long[] arr) {

System.out.print ("[");

for (long Num:arr) {

System.out.print (num + "");

}

System.out.print ("]");

System.out.println ();

}

public static void sort (long[] arr, int left, int. right) {

if (right-left <= 0) {

Return

} else {

Set keywords

Long Point = Arr[right];

Get pointcuts and divide arrays

int partition = partition (arr, left, right, point);

Quickly sort the sub-array on the left

Sort (arr,left,partition-1);

Quickly sort the sub-array on the right

Sort (arr,partition + 1, right);

}

}

}

Test:

public class Testquicksort {

public static void Main (string[] args) {

long[] arr = new LONG[10];

for (int i = 0; i < 10;i++) {

Arr[i] = (long) (Math.random () * 99);

}

Quicksort.displayarr (arr);

Quicksort.sort (arr, 0, arr.length-1);

Quicksort.displayarr (arr);

}

}


This article is from the "8159085" blog, please be sure to keep this source http://8169085.blog.51cto.com/8159085/1696506

Grinding data structures and algorithms-09 Quick Sort

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.