随机生成并排序 C,去同,有序数组合并排序
1 #include<iostream> 2 #include<stdlib.h> 3 #include<time.h> 4 using namespace std; 5 6 int main(void) 7 { 8 9 int array[100]; 10 int RANGE_MIN = 0; 11 int RANGE_MAX = 100; 12 for (int i = 0; i < 100; i++) 13 { 14 int rand100 = (((double) rand() / 15 (double) RAND_MAX) * RANGE_MAX + RANGE_MIN); 16 cin >> array[i]; 17 18 } 19 int temp; 20 21 for (int i = 0; i < 100; i++) 22 for (int j = 0; j < 99 - i; j++) 23 { 24 if (array[j] > array[j + 1]) 25 { 26 temp = array[j]; 27 array[j] = array[j + 1]; 28 array[j + 1] = temp; 29 } 30 } 31 for (int i = 0; i < 100; i++) 32 cout << array[i] << " "; 33 system("pause"); 34 }
1 #include<iostream> 2 #include<stdlib.h> 3 #include<time.h> 4 using namespace std; 5 6 int main(void) 7 { 8 int *arrayflag = new int; 9 int array[100]; 10 int RANGE_MIN = 0; 11 int RANGE_MAX = 100; 12 for (int i = 0; i < 100; i++) 13 { 14 int rand100 = (((double) rand() / 15 (double) RAND_MAX) * RANGE_MAX + RANGE_MIN); 16 array[i]=rand100; 17 18 } 19 int temp; 20 21 for (int i = 0; i < 100; i++) 22 for (int j = 0; j < 99 - i; j++) 23 { 24 if (array[j] > array[j + 1]) 25 { 26 temp = array[j]; 27 array[j] = array[j + 1]; 28 array[j + 1] = temp; 29 } 30 } 31 //for (int i = 0; i < 100; i++) 32 //cout << array[i] << " "; 33 int i = 0, left = 0; 34 while (i < 100) 35 arrayflag[i++] = false; //初始化标志数组 36 37 for (i = 0; i < 100; i++)//剔除算法 38 { 39 arrayflag[array[i]] = array[i]; //将出现过的数保存到对应的位置 40 41 } 42 for (i = 0; i < 100; i++) //取出有效数 43 { 44 if (arrayflag[i] != false) 45 { 46 array[left++] = arrayflag[i]; 47 } 48 49 } 50 for (i = 0; i < left-1; i++) 51 cout << array[i] << " "; 52 system("pause"); 53 }
1 #include<iostream> 2 using namespace std; 3 4 int main() 5 { 6 int a[5] = { 1, 5, 7, 9, 25 }, b[5] = { 1, 27, 26, 38, 99 }; 7 int *c = new int; 8 int k=4,j=4; 9 for (int i = 0; i < 10; i++) 10 { 11 if (a[j] > b[k]) 12 { 13 c[i] = a[j]; 14 j--; 15 } 16 else 17 { 18 c[i] =b[k]; 19 k--; 20 } 21 } 22 for (int i = 0; i < 10; i++) 23 cout << c[i] << " "; 24 }