summaryrefslogtreecommitdiff
path: root/c/dataStructure/sorting/bubbleSort.c
diff options
context:
space:
mode:
authorgarhve <git@garhve.com>2022-12-05 19:43:39 +0800
committergarhve <git@garhve.com>2022-12-05 19:43:39 +0800
commitc6bc541ab58363d783e60a007e80e9bf9e231fda (patch)
treea59c7ed0d05225c5876f3e5e919d4f6ed0c447ff /c/dataStructure/sorting/bubbleSort.c
initialize
Diffstat (limited to 'c/dataStructure/sorting/bubbleSort.c')
-rwxr-xr-xc/dataStructure/sorting/bubbleSort.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/c/dataStructure/sorting/bubbleSort.c b/c/dataStructure/sorting/bubbleSort.c
new file mode 100755
index 0000000..521a781
--- /dev/null
+++ b/c/dataStructure/sorting/bubbleSort.c
@@ -0,0 +1,47 @@
+#include<stdio.h>
+#include<stdlib.h>
+#include<time.h>
+
+#define SIZE 10
+
+void swap(int * a, int * b)
+{
+ int tmp = *a;
+ *a = *b;
+ *b = tmp;
+}
+
+void generateRandom(int * arr, int n)
+{
+ time_t sr;
+ srand(sr);
+
+ for (int i = 0; i < n; i++)
+ arr[i] = rand() % 100;
+}
+
+void bubbleSort(int * arr, int n)
+{
+ for (int i = 0; i < n; i++)
+ for (int j = 0; j < n - 1 - i; j++)
+ if (arr[j] > arr[j+1])
+ swap(&arr[j],&arr[j+1]);
+}
+
+int main(void)
+{
+ int arr[SIZE];
+ generateRandom(arr,SIZE);
+
+ for (int i = 0; i < SIZE; i++)
+ printf("%3d",arr[i]);
+ putchar('\n');
+
+ bubbleSort(arr,SIZE);
+
+ for (int i = 0; i < SIZE; i++)
+ printf("%3d",arr[i]);
+ putchar('\n');
+
+ return 0;
+}