Selection sort, insertion sort, and bubble sort)
These three sorting methods are the three basic sorting methods that beginners must know.
Select sort
Objects to be sorted are divided into two parts. One is sorted and the other is unordered. Select a minimum value from the unordered part of the backend,
And put the last one in the sorted part of the front end,
Insert sort
Like playing park Park, we divide the cards into two piles, each time we pull the front card from the back pile of cards, and then insert the proper position of the front pile of cards,
Bubble Sorting
The largest element will be moved to the right side like a bubble. It uses the method of relatively adjacent elements to swap the large element to the right side, so the large element will continue to move to the right, until the appropriate location.
The basic Bubble sorting method can use the flag method to slightly reduce the time for comparison.
Any exchange action indicates that the order has been completed, and the subsequent loop comparison and exchange actions are not required. For example:
Before sorting: 95 27 90 49 80 58 6 9 18 50
27 90 49 80 58 6 9 18 50 [95] 95 surfaced
27 49 80 58 6 9 18 50 [90 95] 90 surfaced
27 49 58 6 9 18 50 [80 90 95] 80 surfaced
27 49 6 9 18 50 [58 80 90 95] ......
27 6 9 18 49 [50 58 80 90 95] ......
6 9 18 27 [49 50 58 80 90 95] ......
6 9 18 [27 49 50 58 80 90 95]
As there will be no exchange action next, sorting ends early. In the above example, we also add the idea that
When the rows reach I + 1, there is no exchange action, indicating that the next I + 2 to N has been sorted, which also improves the efficiency of Bubble sorting.
# Include <stdio. h>
# Include <stdlib. h>
# Define max 10
# Define swap (x, y) {int t; t = x; X = y; y = T ;}
Void selsort (INT []); // select sorting
Void insort (INT []); // insert sorting
Void bubsort (INT []); // Bubble Sorting
Int main (void)
{
Int number [Max] = {0 };
Int I;
Srand (Time (null ));
Printf ("Before sorting :");
For (I = 0; I <Max; I ++)
{
Number [I] = rand () % 100;
Printf ("% d", number [I]);
}
Printf ("\ n -------- select the sorting method ------- \ n ");
Printf ("(1) bubble sort (2) Select sort (3) insert sort \ N input number :");
Scanf ("% d", & I );
Switch (I)
{
Case 1:
Bubsort (number );
Break;
Case 2:
Selsort (number );
Break;
Case 3:
Insort (number );
Break;
Default:
Printf ("option error (1 .. 4) \ n ");
}
Return 0;
}
// Insert sorting
Void insort (INT number [])
{
Int I, J, K, TMP;
For (j = 1; j <Max; j ++)
{
TMP = number [J];
I = J-1;
While (TMP <number [I])
{
Number [I + 1] = number [I];
I --;
If (I =-1)
Break;
}
Number [I + 1] = TMP;
Printf ("% d sorting:", J );
For (k = 0; k <Max; k ++)
Printf ("% d", number [k]);
Printf ("\ n ");
}
}
// Select sorting
Void selsort (INT number [])
{
Int I, J, K, M;
For (I = 0; I <MAX-1; I ++)
{
M = I;
For (j = I + 1; j <Max; j ++)
If (number [J] <number [m])
M = J;
If (I! = M)
Swap (number [I], number [m])
Printf ("% d sorting:", I + 1 );
For (k = 0; k <Max; k ++)
Printf ("% d", number [k]);
Printf ("\ n ");
}
}
// Bubble sort
Void bubsort (INT number [])
{
Int I, J, K, flag = 1;
For (I = 0; I <MAX-1 & flag = 1; I ++)
{
Flag = 0;
For (j = 0; j <MAX-i-1; j ++)
{
If (number [J + 1] <number [J])
{
Swap (number [J + 1], number [J]);
Flag = 1;
}
}
Printf ("% d sorting:", I + 1 );
For (k = 0; k <Max; k ++)
Printf ("% d", number [k]);
Printf ("\ n ");
}
}