Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. #include"stdio.h"
  2. #include"stdlib.h"
  3. #include"stdbool.h"
  4. #include"time.h"
  5. #define SIZE2 100
  6. #define SIZE 20
  7. void BubbleSort(int arr[], int size);
  8. void LessEqualSorted(int arr[], int size);
  9. void Selection_Sort(int arr[], int size);
  10. void number_generator(int arr[], int size);
  11. int main() {
  12.  
  13. int arr[SIZE] = { 11, 10, 11, 19, 14, 2, 18, 11, 17, 20, 16, 20, 16, 3, 3, 6, 9, 17, 16, 4 };
  14. //int arr[SIZE];
  15. //number_generator( arr, SIZE);
  16. printf_s("Unsortiertes Array::\n");
  17. for (int i = 0; i < SIZE; i++) {
  18. if (i == SIZE-1) {
  19. printf_s("%d \n", arr[i]);
  20. }
  21. else printf_s("%d, ", arr[i]);
  22. }
  23.  
  24.  
  25. LessEqualSorted(arr, SIZE);
  26.  
  27. BubbleSort(arr, SIZE);
  28.  
  29. printf_s("Sortiertes Array (Bubble-Sort):\n");
  30. for (int i = 0; i < SIZE; i++) {
  31. if (i == SIZE - 1) {
  32. printf_s("%d \n", arr[i]);
  33. }
  34. else printf_s("%d, ", arr[i]);
  35. }
  36.  
  37.  
  38. LessEqualSorted(arr, SIZE);
  39.  
  40. printf_s("\n\n");
  41. //int arr2[SIZE2] = { -10, -4, -4, 8, 3, 3, -9, -8, 3, 7, 4, -8, 9, 6, -4, -7, 3, 3, -9, -10 };
  42. int arr2[SIZE2];
  43.  
  44. number_generator(arr2, SIZE2);
  45. printf_s("Unsortiertes Array::\n");
  46. for (int i = 0; i < SIZE2; i++) {
  47. if (i == SIZE2 - 1) {
  48. printf_s("%d \n", arr2[i]);
  49. }
  50. else printf_s("%d, ", arr2[i]);
  51. }
  52. //printf_s("Das Array ist NICHT sortiert!\n");
  53.  
  54. LessEqualSorted(arr2, SIZE2);
  55.  
  56. Selection_Sort(arr2, SIZE2);
  57. printf_s("Sortiertes Array (selection-Sort):\n");
  58. for (int i = 0; i < SIZE2; i++) {
  59. if (i == SIZE2 - 1) {
  60. printf_s("%d \n", arr2[i]);
  61. }
  62. else printf_s("%d, ", arr2[i]);
  63. }
  64. LessEqualSorted(arr2, SIZE2);
  65. return 0;
  66.  
  67. }
  68.  
  69. void BubbleSort(int arr[], int size) {
  70. int temp = 0;
  71.  
  72. for (int j = 0; j <= size; j++) {
  73. for (int i = 0; i < size; i++) {
  74. if (i == size - 1) {
  75. break;
  76. }
  77. else if (arr[i] > arr[i + 1]) {
  78. temp = arr[i];
  79. arr[i] = arr[i + 1];
  80. arr[i + 1] = temp;
  81. }
  82. }
  83. }
  84. }
  85. void Selection_Sort(int arr[], int size) {
  86. int temp = 0;
  87. int min = 0;
  88. int min_pos = 0;
  89.  
  90. for (int i = 0; i < size; i++) {
  91.  
  92. min = arr[i];
  93.  
  94. for (int j = i + 1; j < size; j++) {
  95.  
  96. if (min > arr[j]) {
  97. min = arr[j];
  98. min_pos = j;
  99. }
  100. }
  101.  
  102. if (arr[i] > min) {
  103. temp = arr[i];
  104. arr[i] = min;
  105. arr[min_pos] = temp;
  106. }
  107. }
  108. }
  109.  
  110. void number_generator(int arr[], int size) {
  111. srand(time(NULL));
  112. int rand_num;
  113. for (int i = 0; i < size; i++) {
  114. rand_num = (int)rand() % SIZE2;
  115. arr[i] = rand_num;
  116. }
  117. }
  118.  
  119. void LessEqualSorted(int arr[], int size) {
  120. int temp = 0;
  121. bool sorted = 0; //false
  122.  
  123. for (int j = 0; j <= size; j++) { //erste Schleife
  124. for (int i = 0; i <= size; i++) { //Zweite Schleife
  125. if (i == size-1) {
  126. break;
  127. }
  128. else if (arr[i] > arr[i + 1]) {
  129. sorted = 0;
  130. break; //ist für jede schleife
  131. }
  132. else sorted = 1;
  133. }
  134.  
  135. if (sorted == 0) {
  136. printf_s("False \n");
  137. printf_s("Das Array ist NICHT sortiert!\n");
  138. break;
  139. }
  140. }
  141.  
  142. if (sorted == 1) {
  143. printf_s(" True \n");
  144. printf_s("Das Array ist aufsteigend sortiert.\n");
  145. //exit(0);
  146. }
  147. }
  148.  
  149.  
  150.  
  151.  
  152. // Programm ausführen: STRG+F5 oder "Debuggen" > Menü "Ohne Debuggen starten"
  153. // Programm debuggen: F5 oder "Debuggen" > Menü "Debuggen starten"
  154.  
  155. // Tipps für den Einstieg:
  156. // 1. Verwenden Sie das Projektmappen-Explorer-Fenster zum Hinzufügen/Verwalten von Dateien.
  157. // 2. Verwenden Sie das Team Explorer-Fenster zum Herstellen einer Verbindung mit der Quellcodeverwaltung.
  158. // 3. Verwenden Sie das Ausgabefenster, um die Buildausgabe und andere Nachrichten anzuzeigen.
  159. // 4. Verwenden Sie das Fenster "Fehlerliste", um Fehler anzuzeigen.
  160. // 5. Wechseln Sie zu "Projekt" > "Neues Element hinzufügen", um neue Codedateien zu erstellen, bzw. zu "Projekt" > "Vorhandenes Element hinzufügen", um dem Projekt vorhandene Codedateien hinzuzufügen.
  161. // 6. Um dieses Projekt später erneut zu öffnen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement