-
Notifications
You must be signed in to change notification settings - Fork 0
/
Random Bubble Sort.c
executable file
·77 lines (66 loc) · 1.46 KB
/
Random Bubble Sort.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
//Caleb Elliott
//bubble.c
//famous bubble sort
//implement the swap algorithm with pointers
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define MAX 9
//function prototypes
void printValues();
void sort();
void swap(int*,int*);
int generate();
int values[MAX];
int main(){
generate();
printf("Before: \n");
printValues();
sort();
printf("After: \n");
printValues();
return (0);
}//End main
int generate()
{
int i;
srand(time(NULL));
for (i = 0; i<(MAX-1); i++)
values[i] = (rand() % (MAX-0+1));
printValues();
return *values;
}
void sort(){
int i,j;
for (i = 0; i < (MAX-1); i++)//for loop for place i
{
for (j = 0; j <(MAX-1); j++)//for loop for place j
{
if (values[j] > values[j+1])
{//compares spot i to spot j
//printf("j: %d \n", values +j);
//printf("*j: %d \n\n", *(values +j));
swap(values+j, values+j+1);//call swap
printValues();
}
}
}
}
void swap(int *x, int *y){//swaps the two pointers
//printf("I got to swap");
int hold;
hold = *x;
//printf("Hold: %d \n", hold);
//printf("*x: %d \n", *x);
//printf("x: %d \n", x);
*x = *y;
*y = hold;
}
void printValues(){//prints the array
int i,j;
for (i = 0; i < (MAX); i++)
{
printf("%d ", values[i]);
}
printf("\n");
}