Sorting algorithm Selection

Source: Internet
Author: User

The choice of sorting is also a relatively simple sort method, which is a kind of internal ordering, the implementation of the idea is very simple.
Internal sorting: Refers to the size of the table is small enough to be able to put it all in memory to sort the method. (insert sort, quick sort, heap sort, merge sort, cardinality sort, etc.).
External sorting: When the data information being sorted is too large to fit into memory, the file must be segmented into memory until the entire file is automatic arranging ordered.

Basic ideas

In the set of numbers to be sorted, select the smallest number to exchange with the first position, and then in the remaining number, find the smallest and second position of the number of exchanges, so loop to the penultimate number and the last number comparison.

Analysis

Now assume that a set of data needs to be sorted (from small to Large):
8 10 7 1 5 4 6

Data Sorting Process
8 10 7 1 5 4 6 Raw data
1 10 7 8 5 4 6 First trip
1 4 7 8 5 10 6 Second trip
1 4 5 8 7 10 6 Third Trip
1 4 5 6 7 10 8 Four trips
1 4 5 6 7 8 10 Five Trips
Java implementation
 PackageSortImportJava.util.Random;/** * Select sort * */ Public  class selectsort {     Public Static void Main(string[] args) {int[] array=New int[Ten];        Setrandomvalue (array); System.out.print ("before sorting:");        PrintArray (array);        System.out.println ();        Selectsort (array); System.out.print ("After sorting:");    PrintArray (array); }/** * Simple selection of the basic idea of sorting: * In the group of numbers to be sorted, select the smallest number and the first position of the number of exchanges; * and then in the rest of the number to find the smallest and second position of the number of exchanges, * so loop to the second and last number of the final comparison     。 * * @param Array * *     Public Static void Selectsort(int[] array) {intPosition =0;//Used to record your current location         for(inti =0; i < Array.Length; i++) {//Iterate through the array (the array representation before I is already from small to large)            intj = i +1; Position = i;inttemp = Array[i];//Assuming the current position is the minimum value (compared to the subsequent number, if there is a smaller swap)             for(; J < Array.Length; J + +) {//Traverse the number following the array starting at the current position                if(Array[j] < temp) {//Determine if there is a smaller number than the current positiontemp = Array[j];//If there is, a smaller numberPosition = j;            }} Array[position] = Array[i];        Array[i] = temp; }    }/** * Print all elements of an array * /     Public Static void PrintArray(int[] array) { for(inti =0; i < Array.Length; i++) {System.out.print (Array[i] +" "); }    }/** * Randomly generate the value of an array * /     Public Static void  Setrandomvalue(int[] array) {Random r=NewRandom (); for(intI=0; i<array.length-1; i++) {Array[i]=r.nextint ( -);//random generation of 0~100 integers}    } }

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

Sorting algorithm Selection

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.