Advertisement
Guest User

Ja negoo propalice

a guest
Nov 17th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // Sortiranje_Vaja
  4. //
  5. // Created by Tilen Nipic on 17/11/2019.
  6. // Copyright © 2019 Tilen Nipic. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <array>
  11.  
  12. void SelectionSort(int polje[], int velikost);
  13. void QuickSort();
  14. void BubbleSort(int polje[], int velikost);
  15. void swap(int a[], int b[]);
  16. void izpisPolja(int polje[], int velikost);
  17. int partition (int arr[], int low, int high);
  18. void quickSort(int arr[], int low, int high);
  19. using namespace std;
  20.  
  21. int main(int argc, const char * argv[]) {
  22. int polje[20] = {20, 9 ,1, 30, 14, 3, 5, 8, 22, 16, 69, 100, 45, 32, 11, 4, 7, 4, 19, 25};
  23. int velikost = sizeof(polje) / sizeof(polje[0]);
  24. BubbleSort(polje, velikost);
  25. izpisPolja(polje, velikost);
  26. cout << endl;
  27. int polje2[20] = {20, 9 ,1, 30, 14, 3, 5, 8, 22, 16, 69, 100, 45, 32, 11, 4, 7, 4, 19, 25};
  28. SelectionSort(polje2, velikost);
  29. izpisPolja(polje2, velikost);
  30. return 0;
  31. }
  32.  
  33. void swap(int *a, int *b){ // zamenja stevilki v polju. PREK REFERENC SEVEDA
  34. int zacasno;
  35. zacasno = *b;
  36. *b = *a;
  37. *a = zacasno;
  38. }
  39. void izpisPolja(int polje[], int velikost){ // IZPISE POLJE SIMPL KO PASUL
  40.  
  41. for (int i = 0; i < velikost; i++) {
  42. cout << polje[i] << " ";
  43. }
  44. }
  45.  
  46. void BubbleSort(int polje[], int velikost){ //vedno najvecjo postavi na konc vsakic ko prvi for se izvajaa
  47. for (int i = 0; i < velikost - 1; i++) {
  48. for (int j = 0 ; j < velikost - 1 - i; j++) {
  49. if(polje[j] > polje[j + 1])
  50. swap(polje[j], polje[j+1]);
  51. }
  52. }
  53. }
  54.  
  55. void SelectionSort(int polje[], int velikost){ // vsakic postavi najmanjso na zacetek pa se nadaljuje z naslednjim mestom
  56. int minIndex;
  57. for (int i = 0; i < velikost; i++) {
  58. for(int j = i; j < velikost; j++){
  59. if (j == i) {
  60. minIndex = j;
  61. }
  62. if(polje[j] < polje[minIndex])
  63. minIndex = j;
  64. }
  65. swap(polje[minIndex], polje[i]);
  66. }
  67. }
  68. int partition (int arr[], int low, int high) // NIAM POJMA KOPIRO KUJ
  69. {
  70. int pivot = arr[high]; // pivot
  71. int i = (low - 1); // Index of smaller element
  72.  
  73. for (int j = low; j <= high - 1; j++)
  74. {
  75. // If current element is smaller than the pivot
  76. if (arr[j] < pivot)
  77. {
  78. i++; // increment index of smaller element
  79. swap(&arr[i], &arr[j]);
  80. }
  81. }
  82. swap(&arr[i + 1], &arr[high]);
  83. return (i + 1);
  84. }
  85. void quickSort(int arr[], int low, int high)
  86. {
  87. if (low < high)
  88. {
  89. /* pi is partitioning index, arr[p] is now
  90. at right place */
  91. int pi = partition(arr, low, high);
  92.  
  93. // Separately sort elements before
  94. // partition and after partition
  95. quickSort(arr, low, pi - 1);
  96. quickSort(arr, pi + 1, high);
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement