Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Написать программу упорядочения целочисленного массива с
- помощью одного из алгоритмов: обмена, вставок или выбора. Рекомендуемые значения
- длины массива n: 10, 100, 1000, 10000, 100000. Провести проверку упорядоченности.
- */
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
- void test (int n, int a[]) //Проверка упорядоченности
- {
- int i, b[10000];
- for (i=0; i<n; i++) b[i]=a[i];
- for (i=1; i<n; i++)
- {
- if (b[i-1]>b[i])
- {
- printf("Array is not ordered.");
- system("pause");
- exit(0);
- }
- }
- printf("The array is ordered. ");
- }
- void choice (int n, int a[]) //сортировка выбором
- {
- int c, i, j, b[10000], p, min;
- for (i=0; i<n; i++) b[i]=a[i]; //присваиваем значения одного массива другому
- for (i=0; i<n; i++)
- {
- min=b[i];
- c=i;
- for (j=i+1; j<n; j++)
- {
- if (b[j]<min)
- {
- min=b[j];
- c=j;
- }
- }
- p=b[i];
- b[i]=min;
- b[c]=p;
- }
- printf ("\n Sorting by choice: \n");
- for (i=0; i<n; i++) printf ("%d ",b[i]);
- putchar('\n');
- test(n,b); //проверка
- }
- void main()
- {
- const int n=10;
- int a[n];
- srand(time(0));
- for (int i=0; i<n; i++)
- {
- a[i]=rand()%100;
- printf("%d ",a[i]);
- }
- putchar('\n');
- choice(n,a);
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment