Quick ordering of sorting algorithms (Java)

Source: Internet
Author: User

Quick sort public class Quick_sort {///Sort main algorithm private int Partition (int[] data, int start, int end) {int mid_data = data[end];/ /Select the last number as the median sentinel, from the beginning of the traversal, each number compared with the int index = start; The number of records smaller than the Sentinel at the left end of the position or number//note to consider the case of start = 0, and the "Introduction to the algorithm," the pseudo-code has a difference for (int i = start; i < end-1; i++) {//To determine the size relationship of each value to the Sentinel, less than or equal to the The left index position, and the index++//can be understood as the number of values less than Sentinel by index, and all the less than or equal to the number by swapping over the IF (Data[i] <= mid_data) {Swap (data, I, index); index++;}} Move the sentry to the middle, because Data[index] is certainly greater than mid_data, so move to the end still meet the requirements of the algorithm swap (data, index, end); return index; Divide and conquer public void QuickSort (int[] data, int start, int end) {if (Start < end) {///home order, find middle value divide array into half, divide and conquer to continue recursively int mid = Pa Rtition (data, start, end); QuickSort (data, start, mid-1); QuickSort (data, mid + 1, end);}}  Cannot use reference implementations like C + +, so it has to be changed with the data array private void Swap (int[] data, int a, int b) {int temp = Data[a];d ata[a] = data[b];d ata[b] = temp;} public void Print_array (int[] data) {for (int num:data) {System.out.print (num); System.out.print ("");}} public static void Main (string[] args) {inT data[] = {2, 34, 45, 2, 13, 24, 5, 24, 57}; Quick_sort quick_sort = new Quick_sort () quick_sort.quicksort (data, 0, data.length-1);//Note The parameter value of the initial call Quick_sort.print_ Array (data);}}

Quick ordering of sorting algorithms (Java)

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.