Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- /**
- * Funkcja wyswietlajaca tablice
- */
- void printArray(int tab[], int n)
- {
- int i = 0;
- printf("[ ");
- for (i = 0; i < n - 1; i++)
- printf("%d, ", tab[i]);
- printf("%d ]\n", tab[n - 1]);
- }
- /**
- * Funkcja wypelniajaca tablice liczbami losowymi w zakresie min-max
- */
- void fillRandom(int tab[], int n, int min, int max)
- {
- srand(time(NULL));
- int i = 0;
- for (i = 0; i < n; i++)
- {
- tab[i] = (rand() % max) + min;
- }
- }
- /**
- * Sortowanie metoda "flagi polskiej"
- */
- void sort01(int tab[], int n)
- {
- int l = 0;
- int p = n - 1;
- while (l < p)
- {
- while (l < p && tab[l] == 0) l++;
- while (l < p && tab[p] == 1) p--;
- if (l < p)
- {
- int x = tab[l];
- tab[l] = tab[p];
- tab[p] = x;
- l++;
- p--;
- }
- }
- }
- /**
- * Sortowanie metoda "flagi francuskiej"
- */
- void sort012(int tab[], int n)
- {
- int Lo, Mid, Hi, x;
- Lo = 0;
- Mid = 0;
- Hi = n - 1;
- while (Mid <= Hi)
- {
- if (tab[Mid] == 0)
- {
- x = tab[Lo];
- tab[Lo] = tab[Mid];
- tab[Mid] = x;
- Lo++;
- Mid++;
- }
- else if (tab[Mid] == 1) Mid++;
- else
- {
- x = tab[Mid];
- tab[Mid] = tab[Hi];
- tab[Hi] = x;
- Hi--;
- }
- }
- }
- int main()
- {
- const int n = 10; // dlugosc tablic
- const int w = 1; // 1 - wyswietla tablice, 0 - nie wyswietla tablic (tylko czasy)
- int t[n], t2[n];
- int s, f;
- fillRandom(t, n, 0, 2); // wypelnianie tablicy wartosiami z zakresu 0-1
- fillRandom(t2, n, 0, 3); // wypelnianie tablicy wartosiami z zakresu 0-2
- if (w) printArray(t, n);
- if (w) printArray(t2, n);
- printf("\n");
- s = time(NULL);
- sort01(t, n);
- f = time(NULL);
- if (w) printArray(t, n);
- printf("Sortowanie 01: %d s\n", f - s);
- s = time(NULL);
- sort012(t2, n);
- f = time(NULL);
- if (w) printArray(t2, n);
- printf("Sortowanie 012: %d s\n", f - s);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement