Simple sorting (after learning to review in time, otherwise like not to learn the same, and
Spend more time in the future)
Bubble sort: small number up
Bubble sort (bubblesort) is a repeated visit to the sorted sequence,
Compare two elements at a time, if they are in the wrong order, just put them on
Swap them over. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted.
Principle:
1. Compare adjacent elements. If the second one is bigger than the first one, swap them both. (Small in front)
2. The same work for each pair of adjacent elements, from the first pair to the end of the
Last pair. At this point, the last element should be the maximum number.
3. Repeat the above steps for all elements, out of the last one.
4. Repeat the above steps each time for less and fewer elements until
There are no pairs of numbers that need to be compared.
Complexity of Time:
If the initial state of the file is positive, a single scan will complete the sequencing.
Required number of keyword comparisons C and record and record number of moves m
Have reached the minimum value: cmin=n-1;
mmin=0;
So the best time to bubble sort is the complexity of O (n).
Public classbubblesort{ Public Static voidSortLong[] arr) { LongTmp=0; for(inti=0;i<arr.length-1;i++) { //number of trips, and let the smallest in front for(intj=arr.length-1;j>i;j--) { if(arr[j]<arr[j-1]) { //to ExchangeTmp=arr[j-1]; Arr[j-1]=Arr[j]; ARR[J]=tmp; } } } }} Public classtestsort1{ Public Static voidMain (string[] args) {Long[] arr=New Long[5]; arr[0]=34; arr[1]=23; arr[2]=2; arr[4]=1; arr[3]=-4; //If there is no assignment, the back of the direct complement 0System.out.print ("["); for(LongNum:arr) { //use?? System.out.print (num+ ""); } System.out.print ("]"); System.out.println (); Bubblesort.sort (arr); //sort and then printSystem.out.print ("["); for(LongNum:arr) {System.out.print (num+" "); } System.out.print ("]"); System.out.println (); }}/*operation Result: I j[34 2-4 1][-4 1 2]*/
Go back to your dictation once again
Select Sort:
Select minimum to previous and swap position, remaining unchanged
Public classselectionsort1{ Public Static voidSortLong[] arr) { intK=0; LongTmp=0; for(inti=0;i<arr.length-1;i++) { //The number of trips is less than length-1, because the last one is not ordered, which is the largestk=i; //the smallest point number, K to point for(intj=i;j<arr.length;j++) { //why is arr.length instead of length-1?length-1 only to Length-1//the boundary position is still to think about!! Final re-test test if(arr[j]<Arr[k]) {k=J; }} tmp=Arr[i]; Arr[i]=Arr[k]; ARR[K]=tmp; } }} Public classtestsort2{ Public Static voidMain (string[] args) {Long[] arr=New Long[5]; arr[0]=34; arr[1]=23; arr[2]=2; arr[4]=1; arr[3]=-4; //If there is no assignment, the back of the direct complement 0System.out.print ("["); for(LongNum:arr) { //use?? System.out.print (num+ ""); } System.out.print ("]"); System.out.println (); Selectionsort1.sort (arr); //sort and then printSystem.out.print ("["); for(LongNum:arr) {System.out.print (num+" "); } System.out.print ("]"); System.out.println (); }}/*arr.length-1 Cycle Results [2-4 1][-4 2 1]arr.length cycle results [1 2-4 1 2]*/
Direct Insert Sort:
The direct insertion sort is a simple interpolation method, the basic idea is
Insert the records to be sorted by their key code values into one
Ordered sequential sequence, until all records have been inserted,
A new sequence.
For example, a set of records that are known to be sorted is
60,71,49,11,24,3,66
Assuming that the first three records have been incremented by key code values during the sort process
Order to form an ordered sequence:
49,60,71
Insert the fourth record in the sorted record 11 into the required sequence total, to get
A new ordered sequence with 4 records.
1. Set the monitoring R0 to assign the value of the record to be inserted to r0;
2. Set the position of the start search J;
3. Search in the array, and the search will move the first J record back to
R0.key>=[j].key so far;
4. Insert the r0 into the r[j+1] position
Bubble sort and direct insert sort and select sort