Package cn.hncu.dataStruct;
public class Sortmethods {
/*
The evaluation index of the algorithm is good or bad (generally only when the value of n is very large will be considered, such as n take 100,000):
* 1, time complexity: Popular point, refers to the program running Speed (time)---usually use the computer operation (arithmetic, assignment) number of times to replace
* 2, Space complexity: the size of the memory space---usually use in the program how many variables (stack memory, heap memory), the total amount of memory of these variables
*/
public static void Main (string[] args) {
int a[] = {1,7,0,10,8,23,-3,34,102, 21,25,49,25,16,8};
int a[] = {21,25,49,25,16,8,-1,45,230,22};
1 Bubble sort
1.1 Normal bubbling
Bubblesort (a);
1.2 With optimized bubbling
BubbleSort2 (a);
2 Select Sort
2.1 Similar ideas but poor performance--row phone
SelectSort0 (a);
2.2 Real-choice sort
Selectsort (a);
3 Insert Sort
3.1 Simple Insert Sort
Insertsort (a);
3.2 Adding binary to find the insertion sort//※※※ Note: The premise of two points is ordered
Binaryinsersort (a);
4 Hill Sort
Shellsort (a); //
5.1 General Quick Sort
QuickSort (a,0,a.length-1);//Sort the elements in the 0~a.length-1 range in array a
5.1 Quick sorting after optimization
QuickSort2 (a,0,a.length-1);//Sort the elements in the 0~a.length-1 range in array a
6 Merge Sort
MergeSort (a,0,a.length-1);
Print (a);
}
Merging and discharging ordinary sequences
private static void MergeSort (int[] A, int left, int. right) {
if (left<right) {//has at least 2 elements to decompose----if there is no such sentence, a stack overflow error will occur: Stackoverflowerror
First decomposition
int mid = (left + right)/2;//two points
MergeSort (A, left, mid);
MergeSort (A, mid + 1, right);
Re-merge
int b[] = new Int[a.length];
Merge (A, B, left, Mid, right);//Merge two sub-sequences of the current decomposition level into the corresponding position in array b
Copyarray (A, B, left, right);//copy data from auxiliary sequence B back to a
}
}
private static void Copyarray (int[] A, int[] b, int left, int. right) {
for (int i=left;i<=right;i++) {
A[i] = B[i];
}
}
Merge: Combine two ordered sub-sequences into one
private static void merge (Int[] A, int[] b, int left, int mid, Int. right) {
Merge A[left:mid] and A[mid+1:right] to B[left:right]
int p=left;//A cursor traversing a subsequence a[left:mid]
int r=mid+1;//A cursor traversing a subsequence a[mid+1:right]
int k=left; A cursor that merges the result sequence B[left:right]
while (P<=mid && r<=right) {
if (A[p]<a[r]) {
B[k++]=a[p++];
}else{
B[k++]=a[r++];
}
}
After the loop above, the elements of one sequence have been exhausted. Then, the remaining elements of the other subsequence are copied directly into the b[].
if (p>mid) {//left sequence is complete, copy right sequence
for (int i=r;i<=right;i++) {
b[k++] = A[i];
}
}else{//the right sequence, copying the left sequence
for (int i=p;i<=mid;i++) {
b[k++] = A[i];
}
}
}
5.2 Quick sorting after optimization
private static void QuickSort2 (int[] A, int p, int r) {
if (p<r) {
Idea: Divide the array A into two sub-intervals and an element: A[p:q-1], a[q], A[q+1:r]
int q = patition2 (a,p,r);//After the partitioning function, the array becomes: A[p:q-1], a[q], a[q+1:r]----where a[q] position is already lined up.
QuickSort2 (a,p,q-1);//continue to row left sub-range
QuickSort2 (a,q+1,r);//continue to row right sub-range
}
}
private static int Patition2 (int[] A, int p, int r) {
Optimization: Using stochastic selection strategy to determine pivot
int rand =p+ (int) (Math.random () * (r-p));
Swap (A,p,rand);//change the element of the rand position to the P position and then take the elements of the P position as pivots
int i=p;
int j=r+1;
int x=a[p];//pivot is saved in variable x
while (true) {
while (a[++i]<x && i<r);//After this cycle, a[i] is an element greater than x in the left area
while (a[--j]>x);//After this cycle, a[j] is an element less than x in the right area
if (i>=j) {
Break
}
Swap (A,I,J);//a[i] and A[j]
}
Swap the pivot (currently at position P) to the position of J
Swap (A,P,J);
Return J;
}
//////////////////////////////////////////////////////////
5.1 General Quick Sort
private static void QuickSort (int[] A, int p, int r) {
if (p<r) {
Idea: Divide the array A into two sub-intervals and an element: A[p:q-1], a[q], A[q+1:r]
int q = patition (a,p,r);//After the partitioning function, the array becomes: A[p:q-1], a[q], a[q+1:r]----where a[q] position is already lined up.
QuickSort (a,p,q-1);//continue to row left sub-range
QuickSort (a,q+1,r);//continue to row right sub-range
}
}
private static int patition (int[] A, int p, int r) {
int i=p;
int j=r+1;
int x=a[p];//pivot is saved in variable x
while (true) {
while (a[++i]<x && i<r);//After this cycle, a[i] is an element greater than x in the left area
while (a[--j]>x);//After this cycle, a[j] is an element less than x in the right area
if (i>=j) {
Break
}
Swap (A,I,J);//a[i] and A[j]
}
Swap the pivot (currently at position P) to the position of J
Swap (A,P,J);
Return J;
}
4 Hill Sort--in order to make it easy for everyone to understand, the group sort algorithm uses bubbling
private static void Shellsort (int[] a) {
Group
for (int gap= (a.length+1)/2; gap>0;) {
Intra-group Sort---bubbling
for (int i=0;i<a.length-gap;i++) {
for (int j=i;j<a.length-gap;j+=gap) {
if (A[j]>a[j+gap]) {
Swap (A,J,J+GAP);
}
}
}
Correction of cyclic variables
if (gap>1) {
Gap = (gap+1)/2;
}else{
Break
}
}
}
3.2 Insert sort to add binary lookup
private static void Binaryinsersort (int[] a) {
for (int i=0;i<a.length-1; i++) {//number of times, each trip is inserted with the number of "i+1"
Number to insert
int temp = a[i+1];
Use two points to determine where you want to insert
int low=0;
int high=i;
int mid;//Middle Position
while (Low<=high) {
Mid = (Low+high)/2;
Compare the elements in the middle position with the number currently being inserted (temp)
if (a[mid]>temp) {//fall in left half area
High = mid-1;
}else{//falls in the right half of the area
low = mid+1;
}
}
The above section just finds the insertion position.
Shift, freeing up the position to be inserted.
for (int j=i; j>high; j--) {
A[J+1] = A[j];
}
Let temp sit in section high+1.
A[HIGH+1] = Temp;//a[low] = temp;
}
}
3.1 Simple Insert Sort
private static void Insertsort (int[] a) {
Each element is then inserted into the ordered sequence (starting from 2nd, to the last).
for (int i=0;i<a.length-1;i++) {//Number of times
Number to insert
int temp = a[i+1];
The following paragraph actually determines the position of the temp (j+1) and makes the other number larger than the temp move one position.
int j=i;
while (a[j]>temp) {//If the number of "J position" in an ordered sequence is larger than the number to be inserted, move the a[j] back one position
A[J+1] = a[j];//let a[j] move to the j+1 position
j--;
if (j<0) {
Break
}
}
Let temp sit at the back of the number of J, which is the j+1 position.
A[J+1] = temp;
}
}
2.1 Similar ideas but poor performance selection sort---row phone
private static void SelectSort0 (int[] a) {
for (int i=0; i<a.length-1; i++) {//Number of times
for (int j=i+1; j<a.length; J + +) {
if (A[i]>a[j]) {
Swap (A,I,J);
}
}
}
}
2.2 Select Sort
private static void Selectsort (int[] a) {
for (int i=0; i<a.length-1; i++) {//number of times--n-1
int k = i;//*
for (int j=i+1; j<a.length; J + +) {
if (A[k]>a[j]) {//* a[i]-->a[k]
k=j;//*
}
}
* After the above cycle, the K element must be the smallest of the current (from the beginning of the first to the last of these elements)
if (i!=k) {
Swap (A,I,K);
}
}
}
1.2 Bubble sort (optimized)---as long as there is a trip down and there is no exchange, then it is considered to have been sequenced, the following number of trips do not need to go
public static void BubbleSort2 (int[] a) {
for (int i=0;i<a.length-1;i++) {
Boolean isok= true;
for (int j=0;j<a.length-i-1;j++) {
if (A[j]>a[j+1]) {
Swap (a,j,j+1);
IsOk = false;
}
}
if (isOk) {
Break
}
}
}
1.1 Bubble sort
public static void Bubblesort (int[] a) {
for (int i=0;i<a.length-1;i++) {//Number of times: n-1
for (int j=0;j<a.length-i-1;j++) {//Let the number of J and j+1 be compared, the violation of demand is exchanged. J:0~n-i-1
if (A[j]>a[j+1]) {
Swap (a,j,j+1);
}
}
}
}
public static void print (int[] a) {
for (int num:a) {
System.out.print (num+ "\ t");
}
System.out.println ();
}
public static void Swap (Int[]a,int i,int j) {
int temp = A[i];
A[i] = A[j];
A[J] = temp;
}
}
Quick row, bubble row, select Sort, Hill sort