Data Structure --- fast sorting --- java and c ++ implementation

Source: Internet
Author: User

Data Structure --- fast sorting --- java and c ++ implementation

Quick sorting: first, select the first element in a group as the benchmark number. After the first row, the first number before the benchmark number is smaller than the base number, and the second number is greater than the base number. After being divided into two groups, each group is recursive.
For example ):
(1) Raw Data: 6 2 7 3 8 9 I = 0, j = 5
(2) Use 6 as the benchmark and reduce j. If the ratio is 6, the switch is: 3 2 7 6 8 9 I = 0, j = 3.
(3) increase I based on 6. If I is larger than 6, the exchange is: 3 2 6 7 8 9 I = 2, j = 3.

/* Java Implementation ---------------------------------- */package pack; public class Main {public static void main (String [] args) {int [] arr = {57,68, 59,52, 33, 24}; quickSort (arr, 0, arr. length-1); for (int I = 0; I = high) // if not written, it is an endless loop return; int first = low; int last = high; int key = a [first]; // It is based on the key. If it is the first time, it is a [0] while (first
  
   
A [first]) {// move back from front to back first ++;} int temp1 = a [first]; a [first] = a [last]; a [last] = temp1;} quickSort (a, low, first-1); // the first group of recursive quickSort (a, first + 1, high ); // The next group of recursion}/* c ++ implementation ------------------------------------------------ */# include "stdafx. h "# include
   
    
Using namespace std; void Qsort (int a [], int low, int high); int _ tmain (int argc, _ TCHAR * argv []) {int a [] = {57, 68, 59, 52, 72, 28, 96, 33, 24}; Qsort (a, 0, sizeof () /sizeof (a [0])-1); for (int I = 0; I <sizeof (a)/sizeof (a [0]); I ++) {cout <a [I] <
    
     
= High) {return;} int first = low; int last = high; int key = a [first]; while (first <last) {while (first <last & a [last]> = key) {-- last;} int temp = a [first]; a [first] = a [last]; a [last] = temp; while (first <last & a [first] <= key) {++ first;} int temp1 = a [first]; a [first] = a [last]; a [last] = temp1;} Qsort (a, low, first-1); Qsort (a, first + 1, high );}
    
   
  

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.