Study Notes on Sorting

Source: Internet
Author: User

Simple selection and sorting

Idea: select the smallest element, put it in the first position, and then select the smallest element from the remaining elements, and put it in the second position ......... and so on until the sorting is completed.

Package h; public class MyA {static void selectOne (int [] a, int begin) {int p = begin; // assuming the Correction Method for (int I = begin + 1; I



Insert sort

Idea: add new elements to an ordered queue

Public class MyA {// insert entry k to the sequence of the front edge static void insertOne (int [] a, int k) {for (int I = 0; I <= k; I ++) {if (a [I]> = a [k]) {// locate the inserted position int tmp = a [k]; for (int j = K-1; j> = I; j --) a [j + 1] = a [j]; a [I] = tmp; break ;}}} static void show (int [] a) {for (int I = 0; I


Quick sorting

Idea: use one element as the ruler and divide the data into two parts. One is smaller than the size of the ruler element, and the other is greater than or equal to the size of the ruler element. Then perform recursion,

Sort the two blocks in the previous method again quickly until no elements need to be sorted in the partition (Block.

package j;class MyA{static void show(int[] a){for(int i=0; ip1; i--){if(a[i] <= x){a[p1++] = a[i];p2 = i;dr = !dr;continue L1;}}p2 = p1;}else{for(int i=p1; i
   
    = x){a[p2--] = a[i];p1 = i;dr = !dr;continue L1;}}p1 = p2;}}a[p1] = x;quickSort(a,begin,p1-1);quickSort(a,p1+1,end);}public static void main(String[] args){int[] a = {15,22,13,9,16,33,15,23,18,4,33,25,14};show(a);quickSort(a, 0, a.length-1);show(a);}}
   


Hill sorting

Idea: Because the ordering of raw data has a certain impact on the sorting time, data is grouped by a certain step and sorted by insert sorting. The step size is continuously reduced until the step size is 1.

Public class MyA {// One of the hill sorting, dk is the step size static void shellOne (int [] data, int dk) {for (int k = 0; k
   
    
= Data [k + I * dk]) {int tmp = data [k + I * dk]; for (int p = I-1; p> = j; p --) data [k + (p + 1) * dk] = data [k + p * dk]; data [k + j * dk] = tmp; break ;}}}}} static void show (int [] data) {for (int I = 0; I
    
     


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.