Skip to content

Instantly share code, notes, and snippets.

@giljr
Created November 29, 2021 20:11
Show Gist options
  • Save giljr/bf947b5da4cacf40006aee539fbb2d97 to your computer and use it in GitHub Desktop.
Save giljr/bf947b5da4cacf40006aee539fbb2d97 to your computer and use it in GitHub Desktop.
Bubble Sorting
#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