å泡æåºæ³çå
·ä½å®ç°æ¹æ³æ¯è¿æ ·çï¼ä»æ°ç»ç第ä¸ä¸ªå
ç´ `arr[0]`å¼å§ï¼ä¸¤ä¸¤æ¯è¾**(`arr[n],arr[n+1]`)ï¼å¦æåé¢çæ°å¤§äºåé¢çæ°ï¼`arr[n] > arr[n+1]`ï¼,é£ä¹äº¤æ¢ä¸¤ä¸ªå
ç´ çä½ç½®ï¼æ大çæ°å¾å移å¨ãè¿æ ·ä¾æ¬¡ç»è¿ä¸è½®æ¯è¾ä»¥åï¼æ大çæ°å°ä¼è¢«äº¤æ¢å°æåçä½ç½®ï¼arr[n-1]ï¼ã
Cè¯è¨å®ç°Bubblesortï¼
void bubblesort(int a[], int m)
{
int i,j;
int tmp;
int flag = 0; //设å®æ å¿ï¼å¦æ第ä¸æ¬¡å¾ªç¯æ¯è¾æ¶æ²¡æåç交æ¢ï¼å说ææ°ç»æ¯ååºæåºï¼ä¸ç¨æåºï¼æåç»æ循ç¯ã
for(i = 0; i < m; i++) //å¤å±å¾ªç¯æ§å¶å¾ªç¯æ¬¡æ°
{
for(j = 0; j < m-1-i; j++) //å
å±å¾ªç¯æ§å¶æ¯æ¬¡å¾ªç¯éæ¯è¾ç次æ°ã
{
if(a[j] > a[j+1])
{
tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
flag = 1;
}
}
if(0 == flag)
{
printf("No Sort\n");
break;
}
}
}
éæ©æåºæ³çè¿ç¨æ¯ï¼é**è¿æ¯è¾ï¼éæ©åºæ¯ä¸è½®ä¸æå¼å
ç´ ï¼ç¶åæä»åè¿ä¸è½®ä¸ææåé¢çå
ç´ äº¤æ¢**ï¼æ以è¿ä¸ªç®æ³å
³é®æ¯è¦è®°å½æ¯æ¬¡æ¯è¾çç»æï¼å³æ¯æ¬¡æ¯è¾åæå¼ä½ç½®ï¼ä¸æ ï¼ã
Cè¯è¨å®ç°ï¼Selectionsortï¼
void selectionsort(int a[],int m)
{
int i,j;
int k;
int tmp;
for(i = 0; i < m-1; i++)//æ§å¶å¾ªç¯æ¬¡æ°ï¼n个æ°éè¦n-1次循ç¯
{
k = i;
for(j = i+1; j < m ; j++)
{
if(a[j] < a[k])
k = j;
}
//iä¸çäºkæ¯å°±è¯æa[i]ä¸æ¯æå°çï¼
//içäºkæ¶è¯æa[i]å°±æ¯æ¬è½®æ¯è¾è¿ç¨ä¸æå°çå¼
if(i != k)
{
tmp = a[i];
a[i] = a[k];
a[k] = tmp;
}
}
}