Created
November 29, 2021 20:11
-
-
Save giljr/bf947b5da4cacf40006aee539fbb2d97 to your computer and use it in GitHub Desktop.
Bubble Sorting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
void BubbleSort(int vec[]); | |
#define VECTORSIZE 10 | |
int main() | |
{ | |
int vec[VECTORSIZE] = { 0 }; | |
srand(time(NULL)); | |
printf("BUBBLE SORT (ASC)\n"); | |
printf("-----------------\n"); | |
// Data insertion | |
for (int i = 0; i < VECTORSIZE; i++) | |
vec[i] = rand() % 100; | |
printf("Not Ordered Vector:\n"); | |
for (int i = 0; i < VECTORSIZE; i++) | |
printf("%d\t", vec[i]); | |
BubbleSort(vec); | |
// Print results | |
printf("\nOrdered Vector:\n"); | |
for (int i = 0; i < VECTORSIZE; i++) | |
printf("%d\t", vec[i]); | |
printf("\n"); | |
system("pause"); | |
return 0; | |
} | |
void BubbleSort(int vec[]) | |
{ | |
int aux; | |
for (int n = 1; n <= VECTORSIZE; n++) | |
{ | |
for (int i = 0; i < (VECTORSIZE - 1); i++) | |
{ | |
if (vec[i] > vec[i + 1]) | |
{ | |
aux = vec[i]; | |
vec[i] = vec[i + 1]; | |
vec[i + 1] = aux; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment