-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbblSort.c
More file actions
52 lines (40 loc) · 1.02 KB
/
bblSort.c
File metadata and controls
52 lines (40 loc) · 1.02 KB
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
#include <stdio.h>
#include <stdlib.h>
int* bubbleSort(int* arr, int size);
void printArray(int* arr, int size);
int main()
{
int arr[6] = {6,5,4,3,2,1};
int size = sizeof(arr) / sizeof(int);
int* newArr = bubbleSort(arr, size);
printArray(newArr, size);
free(newArr);
return 0;
}
int* bubbleSort(int* arr, int size){
int* newArr = malloc(sizeof(int) * size);
for(int i = 0; i < size; i++){
newArr[i] = arr[i];
}
int size2 = size;
for(int i = 0; i < size; i++){
for(int j = 0; j < size2 - 1; j++){
if(*(newArr + j) > *(newArr + j + 1)){
int temp = *(newArr + j);
*(newArr + j) = *(newArr + j + 1);
*(newArr + j + 1) = temp;
}
}
}
return newArr;
}
void printArray(int* arr, int size){
printf("[");
for(int i = 0; i < size; i++){
if(i == size - 1){
printf("%d]\n", *(arr + i));
break;
}
printf("%d, ", *(arr + i));
}
}