Quick sort in Java

Source: Internet
Author: User

I find it on the Internet, can learn

Quick sort

(1) Basic idea: Select a datum element, usually select the first element or the last element, through a scan, the waiting sequence is divided into two parts, some smaller than the Datum element, part is greater than the datum element, when the datum element in its correct position after the order, Then the same method is used recursively to sort the two parts of the division.

(2) Example:

(3) Implementation in Java
public class QuickSort {inta[]={ 49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};p Ublicquicksort () {quick (a); for (int i=0;i<a.length;i++) {System.out.println (a[i]);}}    Publicint getmiddle (int[] list, int low, int. high) {int tmp =list[low]; The first of the array is used as the mid-axis while (low < high) {and low < high&& List[high] >= tmp) {high--;}   List[low] =list[high]; A record smaller than the middle axis is moved to the lower side while (Low < high&& List[low] <= tmp) {low++;}   List[high] =list[low];              A record larger than the middle axis moved to the high end}list[low] = tmp;                   The middle axis is recorded to the tail return low;   Returns the position of the middle axis}publicvoid _quicksort (int[] list, int low, int.) {if (Low < high) {Int. middle =getmiddle (list, low, high);       Divides the list array into _quicksort (list, low, middle-1);       Recursive ordering of low-word tables _quicksort (List,middle + 1, high); Recursively sort the high-character table}}publicvoid Quick (int[] A2) {if (A2.length > 0) {//See if the array is empty _quicksort (a2,0, a2.length-1);}} 
Merge Sort
(1) Basic sort: merge (merge) sorting method is to combine two (or more than two) ordered tables into a new ordered table, that is, the ordered sequence is divided into several sub-sequences, each sub-sequence is ordered. Then the ordered subsequence is combined into a whole ordered sequence.
(2) Example:

(3) implement
in Java
Import Java.util.arrays;publicclass Mergingsort {inta[]={ 49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};p Ublicmergingsort () {sort (A, 0,A.LENGTH-1); for (int i=0;i<a.length;i++) System.out.println (A[i]);} Publicvoid sort (int[] data, int left, int. right) {//TODO Auto-generatedmethod stubif (left<right) {//Find intermediate index int center= ( Left+right)/2;//recursive sort (data,left,center) on the left array,//recursive sort (data,center+1,right) on the right array,//merging merge (Data,left, center,right);}} Publicvoid merge (int[] data, int left, int center, int. right) {//TODO auto-generatedmethod Stubint [] tmparr=newint[data. Length];int Mid=center+1;//third records the intermediate array's index int third=left;int tmp=left;while (left<=center&&mid<=right {//Remove the smallest of the two arrays into the intermediate array if (Data[left]<=data[mid]) {tmparr[third++]=data[left++];} else{tmparr[third++]=data[mid++];}} The remainder is placed in the middle array in turn (Mid<=right) {tmparr[third++]=data[mid++];} while (Left<=center) {tmparr[third++]=data[left++];} Copies the contents of the intermediate array back to the original array while (Tmp<=right) {Data[tmp]=tmparr[tmp++];} System.out.println (arrays.tostring (data));}}
Base Sort

(1) The basic idea: to unify all the values to be compared (positive integers) to the same digit length, the number of the short number of the first complement 0. Then, start with the lowest bit and order one at a time. Thus, from the lowest bit to the highest order, the sequence becomes an ordered series.

(2) Example:


(3) Implementation in Java

Import Java.util.arraylist;import Java.util.list;public class Radixsort {inta[]={ 49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,101,56,17,18,23,34,15,35,25,53,51};p ublic RadixSort () {sort ( a); for (inti=0;i<a.length;i++) {System.out.println (a[i]);}} public void sort (int[] array) {//first determines the number of times to sort, int max=array[0];for (inti=1;i<array.length;i++) {if (Array[i]>max) { Max=array[i];}} int time=0;//determine the number of digits; while (max>0) {max/=10;time++;} Set up 10 queues; List<arraylist> queue=newarraylist<arraylist> (); for (int i=0;i<10;i++) {arraylist<integer> Queue1=new arraylist<integer> (); Queue.add (queue1);} Time allocation and collection; for (int i=0;i<time;i++) {//allocates array elements; for (intj=0;j<array.length;j++) {//Gets the time+1 number of digits; int x= array[j]% (int) Math.pow (10,i+1)/(int) Math.pow (ten, I); Arraylist<integer>queue2=queue.get (x); Queue2.add (Array[j]); Queue.set (x, queue2);} int count=0;//element counter;//collection queue element; for (int k=0;k<10;k++) {while (Queue.get (k). Size () >0) {arraylist<integer> Queue3=queue.get (k); Array[count]=queUe3.get (0); queue3.remove (0); count++;}}}} 



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Quick sort in 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.