Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int number_of_inversions(int* tab, int n) {
- int inwersji = 0;
- for (int i = 0; i < n - 1; i++) {
- for (int j = i + 1; j < n; j++) {
- if (tab[i] > tab[j]) {
- inwersji++;
- }
- }
- }
- return inwersji;
- }
- int main() {
- srand(time(NULL));//Inicalizacja generatora liczb
- int tabSize = 0;
- int *tab = NULL;
- printf("Please enter size of array: ");
- scanf("%d", &tabSize);
- tab = malloc(tabSize * sizeof(int));//Alokacja pamięci na tablice
- for(int i = 0; i < tabSize; i++)
- {
- tab[i] = rand() % 101;//Wypełnianie tablicy liczbami z przedziału 0-6
- }
- for(int i = 0; i < tabSize; i++)
- {
- printf("\n%d. %d", i, tab[i]);//Wypisanie tablicy
- }
- int number = number_of_inversions(tab, tabSize);
- printf("Inwersji: %d",number);
- free(tab);//Uwolnienie zaalokowanej pamięci
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement