Quick sorting (Java) of sorting algorithms and java of sorting algorithms

Source: Internet
Author: User

Quick sorting (Java) of sorting algorithms and java of sorting algorithms

// Quick sorting public class Quick_Sort {// main sorting algorithm private int Partition (int [] data, int start, int end) {int mid_data = data [end]; // select the final maximum number as the median, and traverse from the beginning. Each number is compared with int index = start; // record the position or number of numbers smaller than the sentinel on the left. // note that the start value is 0, it is different from the pseudo code in introduction to algorithms. for (int I = start; I <end-1; I ++) {// you can determine the relationship between each value and the size of the Sentinel, if the value is less than or equal to, it is placed at the left index position, and the index ++ // can be understood as using the index to calculate the number of values smaller than the Sentinel value, and all the numbers smaller than or equal to them are moved over through the switch if (data [I] <= mid_data) {swap (data, I, index); index ++ ;}} // move the sentinel to the center. Because data [index] must be greater than mid_data, swap (data, index, end); return index ;} // divide and conquer public void QuickSort (int [] data, int start, int end) {if (start <end) {// sort the original address, locate the median to split the array into two parts, recursive int mid = Partition (data, start, end); QuickSort (data, start, mid-1); QuickSort (data, mid + 1, end );}} // The reference implementation cannot be used like C ++, so you have to use the data array to change the private void swap (int [] data, int a, int B) {int temp = data [a]; data [a] = data [B]; data [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 initial call parameter value quick_Sort.print_array (data );}}

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.