本篇文章以C代碼爲例,能夠直接在編譯器上使用。
實現步驟:數組
代碼實現(有些變量沒有使用)ide
#include <stdio.h> void collect(int* p); void collect(int* p) { int temp1, temp2, temp3; int arr[3];//用來存放收集的數 temp1 = *p; temp2 = *(p + 1); temp3 = *(p + 2); if (temp1 == temp2 && temp2 == temp3) ; else if (temp1 == temp2 || temp1 == temp3 || temp2 == temp3) { if (temp1 == temp2)//若是一、2數相等,比較3數與1數 { if (temp1 < temp3) ;//輸出1<2<3 else//1>3 1=2>3 將1與3對調 { int temp = 0; temp = *p; *p = *(p + 2); *(p + 2) = temp; }//1<2=3 } else if (temp1 == temp3) { if (temp2 < temp3)//2<1=3 1與2對調 { int temp = 0; temp = *p;//5 255 *p = *(p + 1);//2 *(p + 1) = temp;//5 } else//1=3<2 將2與3對調 { int temp = 0; temp = *(p + 1); *(p + 1) = *(p + 2); *(p + 2) = temp; } } else//2=3 { if (temp1 < temp3) ;//1<2=3 else//2=3<1 將1與3對調 { int temp = 0; temp = *p; *p = *(p + 2); *(p + 2) = temp; } } } else { if (temp1 > temp2 && temp1 > temp3) { if (temp2 > temp3)//3<2<1 1 3調換 { int temp = 0; temp = *p; *p = *(p + 2); *(p + 2) = temp; } else//2<3<1 { *p = temp2; *(p + 1) = temp3; *(p + 2) = temp1; } } else if (temp2 > temp1 && temp2 > temp3) { if (temp1 > temp3)//3<1<2 { *p = temp3; *(p + 1) = temp1; *(p + 2) = temp2; } else//1<3<2 2 3對調 { int temp = 0; temp = *(p + 1); *(p + 1) = *(p + 2); *(p + 2) = temp; } } else//3最大 { if (temp1 > temp2)//2<1<3 { *p = temp2; *(p + 1) = temp1; } else//1<2<3 ; } } } int main() { int arr[13] = { 1,3,5,7,9,10,25,26,30,35 }; int i, j, min, max, count; int letin[3]; for (i = 0; i < 10; i++) { printf("%4d", arr[i]); } printf("請輸入你要添得3個數:>"); //scanf("%d", &number);//8 for (i = 0; i < 3; i++) { scanf("%d", &letin[i]); } collect(letin);//將插入的數進行排序 min = letin[0]; max = letin[2]; //輸入的最小值>原數列的最大值 //輸入的最大值<原數列的最小值 //直接輸出 if (min > 35 || min == 35) { arr[10] = letin[0]; arr[11] = letin[1]; arr[12] = letin[2]; } else if (max < 1)//先定義一個新數組接收,在將arr重置 { int newarr[13]; for (i = 0; i < 3; i++) { newarr[i] = letin[i]; } for (j = 3; j < 13; j++) { newarr[j] = arr[j - 3]; } for (i = 0; i < 13; i++) { arr[i] = newarr[i]; } } else//分兩種狀況:第一種letin3個數在arr中兩個數之間;第二種無序 { int newletin[3], m, n; int newarr[13]; i = 0; //第一種 while ( i < 10) { if (arr[i]<min && arr[i + 1]>max)//arr[i]<min<max<arr[i+1] {//1 3 5 7 9 10 25 11 12 13(9) ,26,30,35 int newarr[13], k, m, n; for (j = 0; j <= i; j++) { newarr[j] = arr[j]; } for (j = i + 1; j <= i + 3; j++) {//7 6 7 7-7 7-6 7-5 newarr[j] = letin[j - i - 1];//7 0 } for (j = i + 4; j < 13; j++) {// 10 7 newarr[j] = arr[j - 3];//26 } for (n = 0; n < 13; n++) { arr[n] = newarr[n]; } //break; } i++; } count = 0; for (j = 0; j < 3; j++)//無序 { for (i = 0; i < 10; i++) {//1,3,5,7,9,10,25,26,30,35 4 8 11 if (arr[i] > letin[j]) { newletin[j] = i;//2 4 6 break; } } // 0,1,2,3,4,5,6, 7, 8, 9,10,11,12 }// 1,3,4,5,7,8,9,10,11,25,26,30,35 for (m = 0; m < newletin[0]; m++)//[0,2) { newarr[m] = arr[m];//0 1->1 3 1 3 } newarr[newletin[0]] = letin[0];// 2 /////////////////////////////////////////////// 4 for (m = newletin[0]+1; m <= newletin[1]; m++)//[3,4) {/////////[3,4] 2,3 newarr[m] = arr[m-1];//1,3,5,7,9,10,25,26,30,35 } //0,1,2,3,4, 5, 6, 7, 8, 9 newarr[newletin[1]+1] = letin[1];//5 8 for (m = newletin[1]+2; m <= newletin[2]+1; m++)//[5,6) {///////m=6 m=7 newarr[m] = arr[m-2]; } newarr[newletin[2]+2] = letin[2];//8 for (m = newletin[2]+3; m < 13; m++)//[7,13) {///m=9 m<13 newarr[m] = arr[m-3]; } //newarr存有所有元素 for (i = 0; i < 13; i++) arr[i] = newarr[i]; } for (i = 0; i < 13; i++) printf("%d ", arr[i]); return 0; }
在主函數中的if - else if - else
else語句中使用了一個while語句和for語句。函數
while語句的做用是判斷輸入的三個數是否在原數組的兩個數之間。若是遍歷了原數組和輸入數組發現條件不成立,就結束while語句。
進入for語句,進行最後的查找排序。
注:在代碼中有不少數字,數字的做用是來判斷數組的下標和元素code