1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
#include <iostream>
#include <cstdlib>
using std::cout;
using std::cin;
void copy(int* a, int n);
void selectionSort(int* a, int n);
void doubleSort(int* a, int n);
void insertionSort(int* a, int n);
void mergeSort(int* a, int n);
main()
{
int n;
do {
cout << "Koliko elemenata? ";
cin >> n;
} while (n<2);
int *a = new int[n], *s = new int[n];
if(n>4) cout << "Unesite " << n << " celih brojeva:\n";
else cout << "Unesite " << n << " cela broja:\n";
for(int x = 0; x!=n; x++) cin >> a[x];
cout<<'\n';
for(int x = 0; x<n; x++) s[x] = a[x];
selectionSort(s,n);
for(int x = 0; x<n; x++) s[x] = a[x];
doubleSort(s,n);
for(int x = 0; x<n; x++) s[x] = a[x];
insertionSort(s,n);
delete a;
delete s;
cin.get();cin.get();
return 0;
}
void selectionSort(int* a, int n)
{
int b = 4;
for(int k = n-1, x=0, m; x<k; x++)
{
b += 6;
m = x;
for(int y = x+1; y<n; y++)
{
b += 4;
if(a[m] > a[y]) { m = y; b++; }
}
if(x != m)
{
int h = a[x];
a[x] = a[m];
a[m] = h;
b += 3;
}
}
for(int x = 0; x!=n; x++) cout << a[x] << " ";
cout << "\nZa sortiranje ovog niza Selection Sort algoritmom uradjeno je "
<< b << " operacija.\n\n";
}
void doubleSort(int* a, int n)
{
int b = 3;
for(int x = n-1; x>1; x--)
{
b += 4;
for(int y = 1; y <= x; y++)
{
b += 5;
int u = y-1;
if(a[y] < a[u])
{
int h = a[y];
a[y] = a[u];
a[u] = h;
b += 3;
}
}
}
for(int x = 0; x!=n; x++) cout << a[x] << " ";
cout << "\nZa sortiranje ovog niza Double Sort algoritmom uradjeno je "
<< b << " operacija.\n\n";
}
void insertionSort(int* a, int n)
{
int b = 2;
for(int m, x = 1; x<n; x++)
{
b += 2;
m = a[x];
for(int y = x; ; y--)
{
b += 4;
if (y==0 || a[y-1] <= m) { a[y] = m; break; b++; }
else { a[y] = a[y-1]; b += 2; }
}
}
for(int x = 0; x!=n; x++) cout << a[x] << " ";
cout << "\nZa sortiranje ovog niza Insertion Sort algoritmom uradjeno je "
<< b << " operacija.\n\n";
}
void mergeSort(int* a, int n)
{
}
|