Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5.  
  6. void bubblesort(int tab[], int n) //funkcja sortowania b¹belkowego zbudowana iteracyjnie
  7. {
  8. int i,j;
  9. for(i=1;i<n;i++){
  10. for(j=n-1;j>=1;j--){
  11. if(tab[j]<tab[j-1]){
  12. int swap;
  13. swap=tab[j-1];
  14. tab[j-1]=tab[j];
  15. tab[j]=swap;
  16. }
  17. }
  18. }
  19. }
  20.  
  21. void fill(int tab[], int n) //funkcja która uzupe³nia tablicê losowymi liczbami z zakresu -10 do 10
  22. {
  23. srand(time(NULL));
  24. int i;
  25. for(i=0;i<n;i++){
  26. tab[i]=(rand()%21)-10;
  27. }
  28. }
  29.  
  30. void fill_zakres(int tab[], int n)
  31. {
  32. srand(time(NULL));
  33. int i;
  34.  
  35. int a,b,y,z;
  36. printf("Podaj zakres losowania liczb:\n");
  37. printf("Od: ");
  38. scanf("%d", &a);
  39. printf("Do: ");
  40. scanf("%d", &b);
  41. printf("Zakres %d %d\n", a,b);
  42. y=a;
  43. z=b;
  44. if(a<0){
  45. y=a*(-1);
  46. }
  47. if(b<0){
  48. z=b*(-1);
  49. }
  50. printf("y: %d z: %d\n", y,z);
  51.  
  52. int x;
  53. x=y+z+1;
  54. printf("x: %d", x);
  55.  
  56. for(i=0;i<n;i++){
  57. tab[i]=(rand()%x)+a;
  58. }
  59. system("cls");
  60. printf("Zakres %d %d\n", a,b);
  61.  
  62. }
  63.  
  64. void print_tab(int tab[],int n) //funkcja wypisujuj¹ca zawartoœæ tablicy
  65. {
  66. int i;
  67. for(i=0;i<n;i++){
  68. printf("%d ",tab[i]);
  69. }
  70. }
  71.  
  72. int main()
  73. {
  74. clock_t start,stop;
  75. double czas;
  76. int numer;
  77.  
  78. while(1){
  79. system("cls");
  80. printf("Co chcesz zrobic: \n");
  81.  
  82. printf("1.Mierzenie czasu sortowania babelkowego dla n elemenow\n");
  83. printf("2.Wyswietlenie tablicy przed i po sortowaniu dla n elementow\n");
  84. printf("3.Zakoncz dzialanie programu \n");
  85. scanf("%d", &numer);
  86.  
  87. if(numer==1){
  88. system("cls");
  89.  
  90. int n;
  91. printf("Ile liczb wylosowac: ");
  92. scanf("%d", &n); //pobranie od u¿ytkownika iloœci losowanych liczb
  93.  
  94. int tab[n];
  95. fill_zakres(tab,n); //uzupe³nienie tablicy
  96.  
  97. start = clock();
  98. bubblesort(tab,n);
  99. stop = clock();
  100. czas = (double)(stop-start)/CLOCKS_PER_SEC; //wywo³anie funkcji sortuj¹cej tablice
  101. printf("Posortowanie metoda babelkowa %d liczb zajmie: %lf s\n",n,czas);
  102. puts("");
  103. system("pause");
  104. }
  105. else if(numer==2){
  106. system("cls");
  107. int n;
  108. printf("Ile liczb wylosowac: ");
  109. scanf("%d", &n); //pobranie od u¿ytkownika iloœci losowanych liczb
  110.  
  111. int tab[n];
  112. fill_zakres(tab,n); //uzupe³nienie tablicy
  113. puts("\nPrzed sortowaniem :");
  114. print_tab(tab,n); //wypisanie przed wywo³aniem funkcji do sortowania
  115. puts("");
  116. puts("Po sortowaniu: ");
  117. bubblesort(tab,n); //wywo³anie funkcji sortuj¹cej tablice
  118. print_tab(tab,n); //ponowne wypisanie tablicy
  119. puts("");
  120. system("pause");
  121. }
  122. else if(numer == 3){
  123. system("cls");
  124. printf("Zakonczono dzialanie.");
  125. return 0;
  126. }
  127. else{
  128. system("cls");
  129. puts("Blad! Podaj odpowiednia wartosc.");
  130. system("pause");
  131. }
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement