-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path13_bubblesortasc_v0.c
92 lines (65 loc) · 1.72 KB
/
13_bubblesortasc_v0.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* Project 13 -- BUBBLE SORT (Ascendant Algorithm)
This algorithm works by repeatedly SWAPPING the
ADJACENT ELEMENTS if they are in wrong order.
O(n^2)
Features:
1- Two loops:
-external -> sweep vector size
-internal -> compares 2x2 and SWAP using Auxiliary Variable (aux)
The 1st, Loops to vector size; the 2st, Loops from 1st to penultimate position
and this chank of code is just SWAPPING the ADJACENT ELEMENTS:
aux = vec[i];
vec[i] = vec[i + 1];
vec[i + 1] = aux;
if Ascendant or descendant just THIS SIGN change is necessary:
if (vec[i] > vec[i + 1])
2- Based on: https://github.com/borinvini
UNINTER - Curso: Engenharia da Computacao
Escola Superior Politecnica
Author: Gilberto Jr RU 3326662
Edited: J3
Date: Dec, 2021
Please, watch the gif in this directory > buble_ascending.gif
*/
#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;
}
}
}
}