001
Package algorithm. sort;
002
003
Import java. util. Random;
004
005
/**
006
* <A href = "http://my.oschina.net/arthor" class = "referer" target = "_ blank"> @ author </a> szy
007
* 2012-7-24
008
*/
009
Public class Sort {
010
/**
011
* Select sorting method
012
*
013
* Basic idea: the array to be sorted is divided into two parts,
014
* Some are sorted in ascending order, while some are unordered,
015
* Retrieve the smallest value from the unordered troops and put it at the end of the sorted part.
016
*
017
* @ Param arr
018
* <A href = "http://my.oschina.net/u/556800" class = "referer" target = "_ blank"> @ return </a>
019
*/
020
Public int [] choiceSort (int [] arr ){
021
Int t, I = 0, j;
022
Int len = arr. length;
023
For (; I <len; I ++ ){
024
Int m = I;
025
For (j = I + 1; j <len; j ++ ){
026
// If the j element is smaller than the m element, assign the j value to m.
027
If (arr [j] <arr [m]) {
028
M = j;
029
}
030
}
031
// Swap the positions of m and I elements
032
If (I! = M ){
033
T = arr [I];
034
Arr [I] = arr [m];
035
Arr [m] = t;
036
}
037
}
038
Return arr;
039
}
040
041
/**
042
* Bubble Sorting
043
*
044
* Basic Idea: scanning elements to be sorted from an array
045
* During the scanning process, adjacent elements are compared sequentially, and the elements with large values are moved back.
046
* After each sorting, the element with the maximum value is moved to the end.
047
* Write down the position of the element,
048
* The next sorting only needs to be compared to some locations.
049
* Until all elements are sorted
050
* @ Param arr
051
* <A href = "http://my.oschina.net/u/556800" class = "referer" target = "_ blank"> @ return </a>
052
*/
053
Public int [] bubblingSort (int [] arr ){
054
Int t, I = 0, j = 0;
055
Int len = arr. length;
056
For (; I <len; I ++ ){
057
// Compare the size of two adjacent elements in a loop
058
For (; j <len-i-1; j ++ ){
059
// Compare the size of Adjacent Elements, small forward, large backward
060
If (arr [j]> arr [j + 1]) {
061
T = arr [j];
062
Arr [j] = arr [j + 1];
063
Arr [j + 1] = t;
064
}
065
}
066
}
067
Return arr;
068
}
069
070
/**
071
* Insert sorting
072
*
073
* Basic Idea: divide the sorted array into two parts
074
* Each time, the smallest array element of the index is retrieved from the following array.
075
* Insert it to the appropriate position of the preceding array.
076
* When sorting is started, the first element of the array is regarded as a group, and all elements following it are treated as another group.
077
*
078
* @ Param arr
079
* <A href = "http://my.oschina.net/u/556800" class = "referer" target = "_ blank"> @ return </a>
080
*/
081
Public int [] insertSort (int [] arr ){
082
// View the first element as a part, and the second element as another part.
083
// Insert elements from the second part to the first part in sequence
084
Int I = 1, j;
085
Int len = arr. length;
086
For (; I <len; I ++ ){
087
Int tmp = arr [I];
088
J = I-1;
089
// Compare them with the elements before I in sequence to find the combined insert position
090
While (tmp <arr [j]) {
091
Arr [j + 1] = arr [j];
092
J --;
093
If (j =-1 ){
094
Break;
095
}
096
}
097
// Insert the inserted element to a proper position
098
Arr [j + 1] = tmp;
099
}
100
Return arr;
101
}
102
103
/**
104
* Fast sorting
105
*
106
* Basic Idea: sort a large array into two small arrays.
107
* The sorting of each small array can be further decomposed into two smaller arrays.
108
* Recursive decomposition continues until the maximum array size is 2.
109
*
110
* @ Param arr
111
* @ Param right arr. length-1
112
* @ Param left 0
113
* <A href = "http://my.oschina.net/u/556800" class = "referer" target = "_ blank"> @ return </a>
114
*/
115
Public int [] quickSort (int [] arr, int left, int right ){
116
Int t, len = arr. length;
117
118
If (left <right ){
119
Int s = arr [left];
120
Int I = left;
121
Int j = right + 1;
122
While (true ){
123
// Find an index greater than the number of s to the right
124
While (I + 1 <len & arr [++ I] <s );
125
// Search for an index smaller than the number of s on the left
126
While (J-1>-1 & arr [-- j]> s );
127
// Exit the loop if I> = j
128
If (I> = j)
129
Break;
130
Else {
131
// Switch the I and j locations
132
T = arr [I];
133
Arr [I] = arr [j];
134
Arr [j] = t;
135
}
136
}
137
Arr [left] = arr [j];
138
Arr [j] = s;
139
// Recursion on the left
140
QuickSort (arr, left, J-1 );
141
// Recursion on the right
142
QuickSort (arr, j + 1, right );
143
}
144
Return arr;
145
}
146
147
// Test
148
Public static void main (String [] args ){
149
Int I = 0, len = 100000;
150
Int [] arr = new int [len];
151
Random rd = new Random ();
152
For (; I <len; I ++ ){
153
Arr [I] = rd. nextInt (len );
154
}
155
156
Long millis = System. currentTimeMillis ();
157
// New Sort (). bubblingSort (arr); // 26 seconds
158
// New Sort (). choiceSort (arr); // 287 seconds
159
New Sort (). insertSort (arr); // 172 seconds
160
// New Sort (). quickSort (arr, 0, len-1); // 19 seconds
161
For (I = 0; I <len; I ++ ){
162
System. out. println (arr [I]);
163
}
164
165
System. out. println ("time:" + (System. currentTimeMillis ()-millis)/100
166
+ "Seconds ");
167
}
168
}
By foggy