Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include"stdio.h"
- #include"stdlib.h"
- #include"stdbool.h"
- #include"time.h"
- #define SIZE2 100
- #define SIZE 20
- void BubbleSort(int arr[], int size);
- void LessEqualSorted(int arr[], int size);
- void Selection_Sort(int arr[], int size);
- void number_generator(int arr[], int size);
- int main() {
- int arr[SIZE] = { 11, 10, 11, 19, 14, 2, 18, 11, 17, 20, 16, 20, 16, 3, 3, 6, 9, 17, 16, 4 };
- //int arr[SIZE];
- //number_generator( arr, SIZE);
- printf_s("Unsortiertes Array::\n");
- for (int i = 0; i < SIZE; i++) {
- if (i == SIZE-1) {
- printf_s("%d \n", arr[i]);
- }
- else printf_s("%d, ", arr[i]);
- }
- LessEqualSorted(arr, SIZE);
- BubbleSort(arr, SIZE);
- printf_s("Sortiertes Array (Bubble-Sort):\n");
- for (int i = 0; i < SIZE; i++) {
- if (i == SIZE - 1) {
- printf_s("%d \n", arr[i]);
- }
- else printf_s("%d, ", arr[i]);
- }
- LessEqualSorted(arr, SIZE);
- printf_s("\n\n");
- //int arr2[SIZE2] = { -10, -4, -4, 8, 3, 3, -9, -8, 3, 7, 4, -8, 9, 6, -4, -7, 3, 3, -9, -10 };
- int arr2[SIZE2];
- number_generator(arr2, SIZE2);
- printf_s("Unsortiertes Array::\n");
- for (int i = 0; i < SIZE2; i++) {
- if (i == SIZE2 - 1) {
- printf_s("%d \n", arr2[i]);
- }
- else printf_s("%d, ", arr2[i]);
- }
- //printf_s("Das Array ist NICHT sortiert!\n");
- LessEqualSorted(arr2, SIZE2);
- Selection_Sort(arr2, SIZE2);
- printf_s("Sortiertes Array (selection-Sort):\n");
- for (int i = 0; i < SIZE2; i++) {
- if (i == SIZE2 - 1) {
- printf_s("%d \n", arr2[i]);
- }
- else printf_s("%d, ", arr2[i]);
- }
- LessEqualSorted(arr2, SIZE2);
- return 0;
- }
- void BubbleSort(int arr[], int size) {
- int temp = 0;
- for (int j = 0; j <= size; j++) {
- for (int i = 0; i < size; i++) {
- if (i == size - 1) {
- break;
- }
- else if (arr[i] > arr[i + 1]) {
- temp = arr[i];
- arr[i] = arr[i + 1];
- arr[i + 1] = temp;
- }
- }
- }
- }
- void Selection_Sort(int arr[], int size) {
- int temp = 0;
- int min = 0;
- int min_pos = 0;
- for (int i = 0; i < size; i++) {
- min = arr[i];
- for (int j = i + 1; j < size; j++) {
- if (min > arr[j]) {
- min = arr[j];
- min_pos = j;
- }
- }
- if (arr[i] > min) {
- temp = arr[i];
- arr[i] = min;
- arr[min_pos] = temp;
- }
- }
- }
- void number_generator(int arr[], int size) {
- srand(time(NULL));
- int rand_num;
- for (int i = 0; i < size; i++) {
- rand_num = (int)rand() % SIZE2;
- arr[i] = rand_num;
- }
- }
- void LessEqualSorted(int arr[], int size) {
- int temp = 0;
- bool sorted = 0; //false
- for (int j = 0; j <= size; j++) { //erste Schleife
- for (int i = 0; i <= size; i++) { //Zweite Schleife
- if (i == size-1) {
- break;
- }
- else if (arr[i] > arr[i + 1]) {
- sorted = 0;
- break; //ist für jede schleife
- }
- else sorted = 1;
- }
- if (sorted == 0) {
- printf_s("False \n");
- printf_s("Das Array ist NICHT sortiert!\n");
- break;
- }
- }
- if (sorted == 1) {
- printf_s(" True \n");
- printf_s("Das Array ist aufsteigend sortiert.\n");
- //exit(0);
- }
- }
- // Programm ausführen: STRG+F5 oder "Debuggen" > Menü "Ohne Debuggen starten"
- // Programm debuggen: F5 oder "Debuggen" > Menü "Debuggen starten"
- // Tipps für den Einstieg:
- // 1. Verwenden Sie das Projektmappen-Explorer-Fenster zum Hinzufügen/Verwalten von Dateien.
- // 2. Verwenden Sie das Team Explorer-Fenster zum Herstellen einer Verbindung mit der Quellcodeverwaltung.
- // 3. Verwenden Sie das Ausgabefenster, um die Buildausgabe und andere Nachrichten anzuzeigen.
- // 4. Verwenden Sie das Fenster "Fehlerliste", um Fehler anzuzeigen.
- // 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.
- // 6. Um dieses Projekt später erneut zu öffnen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement