C中的插入排序
Insertion sort in C
有人可以帮帮我吗?我必须在 C 中编写一个插入排序代码来对 10、20、50、.... 数字进行排序,并且我必须使用随机数生成器 rand()。想了好久还是想不起来。我找到了一些代码,您可以在其中选择数字。抱歉我的英语不好。
#include <stdio.h>
int main()
{
int myarray[10];
int i, j, n, temp;
/* Get number of elements in the array */
printf("Enter number of elements in the array \n");
scanf("%d", &n);
/* Read elements of the array */
printf("Enter the array elements \n");
for (i = 0; i < n; i++)
scanf("%d", &myarray[i]);
/* Sort elements of the array */
for (i = 1; i < n; i++) {
j = i;
while ((j > 0) && (myarray[j - 1] > myarray[j])) {
temp = myarray[j - 1];
myarray[j - 1] = myarray[j];
myarray[j] = temp;
j--;
}
}
/* Print the sorted array */
printf("Sorted Array\n");
for (i = 0; i < n; i++)
printf("%d \n", myarray[i]);
return 0;
}
如果您想使用随机数生成器而不是从键盘输入,请添加一些库 headers 并替换您在我评论的地方输入数字的三行代码。我还添加了一行以防止数组溢出。
#include <stdio.h>
#include <stdlib.h> // added library header
#include <time.h> // added library header
int main()
{
int myarray[10];
int i, j, n, temp;
/* Get number of elements in the array */
printf("Enter number of elements in the array \n");
scanf("%d", &n);
if(n < 1 || n > 10) // check range of elements
exit(1);
/* Read elements of the array */
srand((unsigned)(time(NULL))); // seed the random generator
for (i = 0; i < n; i++) // each element
myarray[i] = rand(); // assign a random number
/* Sort elements of the array */
for (i = 1; i < n; i++) {
j = i;
while ((j > 0) && (myarray[j - 1] > myarray[j])) {
temp = myarray[j - 1];
myarray[j - 1] = myarray[j];
myarray[j] = temp;
j--;
}
}
/* Print the sorted array */
printf("Sorted Array\n");
for (i = 0; i < n; i++)
printf("%d \n", myarray[i]);
return 0;
}
有人可以帮帮我吗?我必须在 C 中编写一个插入排序代码来对 10、20、50、.... 数字进行排序,并且我必须使用随机数生成器 rand()。想了好久还是想不起来。我找到了一些代码,您可以在其中选择数字。抱歉我的英语不好。
#include <stdio.h>
int main()
{
int myarray[10];
int i, j, n, temp;
/* Get number of elements in the array */
printf("Enter number of elements in the array \n");
scanf("%d", &n);
/* Read elements of the array */
printf("Enter the array elements \n");
for (i = 0; i < n; i++)
scanf("%d", &myarray[i]);
/* Sort elements of the array */
for (i = 1; i < n; i++) {
j = i;
while ((j > 0) && (myarray[j - 1] > myarray[j])) {
temp = myarray[j - 1];
myarray[j - 1] = myarray[j];
myarray[j] = temp;
j--;
}
}
/* Print the sorted array */
printf("Sorted Array\n");
for (i = 0; i < n; i++)
printf("%d \n", myarray[i]);
return 0;
}
如果您想使用随机数生成器而不是从键盘输入,请添加一些库 headers 并替换您在我评论的地方输入数字的三行代码。我还添加了一行以防止数组溢出。
#include <stdio.h>
#include <stdlib.h> // added library header
#include <time.h> // added library header
int main()
{
int myarray[10];
int i, j, n, temp;
/* Get number of elements in the array */
printf("Enter number of elements in the array \n");
scanf("%d", &n);
if(n < 1 || n > 10) // check range of elements
exit(1);
/* Read elements of the array */
srand((unsigned)(time(NULL))); // seed the random generator
for (i = 0; i < n; i++) // each element
myarray[i] = rand(); // assign a random number
/* Sort elements of the array */
for (i = 1; i < n; i++) {
j = i;
while ((j > 0) && (myarray[j - 1] > myarray[j])) {
temp = myarray[j - 1];
myarray[j - 1] = myarray[j];
myarray[j] = temp;
j--;
}
}
/* Print the sorted array */
printf("Sorted Array\n");
for (i = 0; i < n; i++)
printf("%d \n", myarray[i]);
return 0;
}