There are two processes for these three kinds of sorting:
1. Compare two data.
2. Swap two data or copy one of them.
These three sorts of time levels
Bubble Sort: Compare (N-1) + (N-2) +...+2+1 = N (N-1)/2=N2/2
Exchange 0--N2/2 = N2/4
Total Time 3/4*n2
Select Sort: Compare (N-1) + (N-2) +...+2+1 = N (N-1)/2=N2/2
Exchange 0--3* (N-1) =3* (N-1)/2=3/2*n
Total Time N2/2+3/2*n
Insert sort: The first round is the most comparison, the second round is the most comparison two times, the last round compares N-1 times, so the most compared to n (N-1)/2.
The number of copies and the number of comparisons are roughly equal, but replication consumes less time than swapping.
Compare 0--n* (N-1)/2=n* (N-1)/4=N2/4
Copy 0--n* (N-1)/2=n* (N-1)/4=N2/4
Total Time N2//2
Summary: The insert sorting algorithm is a bit faster than bubbling, slightly faster than the selection sort, but these algorithms are all O (N2) time levels.
These three sorts of ideas
Bubble sort: In the first round, the first and second comparisons, will be large in the back, and then compare the second and third items, will be large in the back, and so on at the end of the first round, the largest data is already in the last item. In a round of comparisons, there are more and more data items in the back row, and fewer items need to be sorted until they are zero.
Select sort: The bubble sort is optimized to reduce the number of exchanges, in the first round the selection of the smallest number is placed in the initial, after one round the first item is ordered, the second round starts from the second item selects the smallest number in the second item, and so on, until the entire array is fully ordered.
Insert Sort: Unlike the first two sorts, the insertion sort is partially ordered during the sort process, and as the number of insertions increases, the position of the items in the ordered section changes, while the bubble sort and the selection sort position of the items per round are always constant. In the first round, select the second item as the INSERT, and then remove the item in a variable, and the previous item is compared and small, then the previous item is moved to the position of the second item, and then the second item is placed in the position of the previous item, the second round selects the third item as the insert item and then the second item is removed The second item is moved back to the insert item, and then inserted as compared to the first item if small, then the first item is moved to the second item, the insert item is placed in the first item, and so on.
These three kinds of sorted Java programs
1. Bubble sort
PackageAA;classbubblesort{Private Long[] A; Private intNelems; PublicBubblesort (intmax) {a=New Long[Max]; Nelems= 0; } Public voidInsertLongvalue) {A[nelems]=value; Nelems++; } Public voiddisplay () { for(inti = 0; i < Nelems; i++) {System.out.println (A[i]+ " "); } System.out.println (""); } Public voidBubblesort () { for(inti = 0; i < nElems-1; i++) { for(intj = 0; J < nElems-1-I; J + +) { if(A[j] > a[j + 1]) { Longtemp =A[j]; A[J]= A[j + 1]; A[j+ 1] =temp; } } } }} Public classBubblesortapp { Public Static voidMain (string[] args) {bubblesort arr=NewBubblesort (100); Arr.insert (77); Arr.insert (99); Arr.insert (44); Arr.insert (55); Arr.insert (22); Arr.insert (88); Arr.insert (11); Arr.insert (00); Arr.insert (66); Arr.insert (33); Arr.display (); Arr.bubblesort (); Arr.display (); } }
2. Select sort
Package Aa;class selectsort{private long[] a;private int nelems;public selectsort (int max) {a = new Long[max];nelems = 0;} public void Insert (Long value) {A[nelems] = Value;nelems + +;} public void display () {for (int i = 0; i < Nelems; i++) {System.out.println (A[i] + "");} System.out.println ("");} public void Selectionsort () {for (int i = 0; i < nElems-1; i++) {int min = i;for (int j = i + 1; j < Nelems; J + +) {if (a [j] < A[min]) {Long temp = a[j];a[j] = a[min];a[min] = temp;}}}} public class Selectsortapp {public static void main (string[] args) {selectsort arr = new Selectsort; Arr.insert (n); AR R.insert (Arr.insert); arr.insert; Arr.insert (+); Arr.insert (n); Arr.insert (one); Arr.insert (00); Arr.insert (Arr.insert); Arr.display (); arr. Selectionsort (); Arr.display ();}}
3. Insert Sort
Package Aa;class insertsort{private long[] a;private int nelems;public insertsort (int max) {a = new Long[max];nelems = 0;} public void Insert (Long value) {A[nelems] = Value;nelems + +;} public void display () {for (int j = 0; J < Nelems; J + +) {System.out.println (A[j] + "");} System.out.println ("");} public void Insertionsort () {int out,in;for (out = 1, out < Nelems, out++) {Long temp = A[out];in = Out;while (in > 0 &A mp;& a[in-1] >= temp) {A[in] = a[in-1];in--;} A[in] = temp;}}} public class Insertsortapp {public static void main (string[] args) {int maxSize = 100;insertsort arr = new Insertsort (maxsi Ze); Arr.insert (Arr.insert); Arr.insert (; arr.insert); Arr.insert (+); Arr.insert (+); Arr.insert (11); Arr.insert (Arr.insert); Arr.insert (77);
Arr.display (); Arr.insertionsort (); Arr.display ();}}
Java object-oriented bubble sorting, select sort and insert sort comparison