The outer loop requires the same number of times as Len.
// Define a function that returns nsstring
Void bubblesort (INT Nums [], unsigned long Len)
{
// Control whether the current cycle has been exchanged
// If there is no exchange, it indicates that the array is already in the ordered state and the sorting can be terminated in advance.
Bool hasswap = yes;
For (INT I = 0; I <Len & hasswap; I ++ ){
// Set hasswap to no
Hasswap = no;
For (Int J = 0; j <len-1-I; j ++ ){
// If Nums [J] is greater than Nums [J + 1], exchange them
If (Nums [J]> Nums [J + 1])
{
Int TMP = Nums [J];
Nums [J] = Nums [J + 1];
Nums [J + 1] = TMP;
// This round of loop is exchanged. Set hasswap to yes.
Hasswap = yes;
}
}
}
}
Int main (INT argc, const char * argv [])
{
@ Autoreleasepool {
Int Nums [] = };
Int Len = sizeof (Nums)/sizeof (Nums [0]);
Bubblesort (Nums, Len );
For (INT I = 0; I <Len; I ++ ){
Printf ("% d \ n", Nums [I]);
}
// Insert code here...
Nslog (@ "Hello, world! ");
}
Return 0;
}
C Bubble Sorting