Skip to content

Commit 55d53a7

Browse files
modify sorting implementation
1 parent 88a520d commit 55d53a7

File tree

6 files changed

+469
-321
lines changed

6 files changed

+469
-321
lines changed
Lines changed: 69 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,81 @@
11
## Bubble Sort
22

3+
![Algorithm Animation](https://images2017.cnblogs.com/blog/849589/201710/849589-20171015223238449-2146169197.gif)
4+
5+
#### Complexity
6+
7+
- Worst and Average Case Time Complexity: O(n*n). Worst case occurs when array is reverse sorted.
8+
- Best Case Time Complexity: O(n). Best case occurs when array is already sorted.
9+
- Auxiliary Space: O(1)
10+
311
#### Code
412
```c
5-
#void swap(int *xp, int *yp)
6-
{
7-
int temp = *xp;
8-
*xp = *yp;
9-
*yp = temp;
10-
}
11-
12-
// An optimized version of Bubble Sort
13-
void bubbleSort(int arr[], int n)
14-
{
15-
int i, j;
16-
bool swapped;
17-
for (i = 0; i < n-1; i++)
18-
{
19-
swapped = false;
20-
for (j = 0; j < n-i-1; j++)
21-
{
22-
if (arr[j] > arr[j+1])
23-
{
24-
swap(&arr[j], &arr[j+1]);
25-
swapped = true;
26-
}
27-
}
28-
29-
// IF no two elements were swapped by inner loop, then break
30-
if (swapped == false)
31-
break;
32-
}
33-
}
34-
35-
/* Function to print an array */
13+
#include <stdlib.h>
14+
#include <stdio.h>
15+
#include <time.h>
16+
17+
typedef void (*sortAlgorithm)(int *array, int size);
18+
19+
void bubblesort(int *array, int size) {
20+
int i, j;
21+
int temp;
22+
23+
for (i = size; i >= 0; i--) {
24+
for (j = 0; j < i-1; j++) {
25+
if (array[j] > array[j+1]) {
26+
temp = array[j];
27+
array[j] = array[j+1];
28+
array[j+1] = temp;
29+
}
30+
}
31+
}
32+
}
33+
3634
void printArray(int arr[], int size)
3735
{
3836
int i;
3937
for (i=0; i < size; i++)
4038
printf("%d ", arr[i]);
41-
printf("n");
39+
printf("\n");
4240
}
43-
44-
// Driver program to test above functions
45-
int main()
46-
{
47-
int arr[] = {64, 34, 25, 12, 22, 11, 90};
48-
int n = sizeof(arr)/sizeof(arr[0]);
49-
bubbleSort(arr, n);
50-
printf("Sorted array: \n");
51-
printArray(arr, n);
52-
return 0;
53-
}
54-
```
5541

56-
#### Complexity
42+
void tests(int *nums, int size) {
43+
sortAlgorithm sort_method = bubblesort;
44+
clock_t start, end;
45+
double cpu_time_used;
5746

58-
- Worst and Average Case Time Complexity: O(n*n). Worst case occurs when array is reverse sorted.
59-
- Best Case Time Complexity: O(n). Best case occurs when array is already sorted.
60-
- Auxiliary Space: O(1)
47+
start = clock();
48+
sort_method(nums, size);
49+
end = clock();
50+
51+
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
52+
53+
printf("==== Sorted array test results ====\n");
54+
printArray(nums, size);
55+
printf("CPU time used: %f\n\n", cpu_time_used);
56+
}
57+
58+
int main() {
59+
// test 1
60+
int nums[] = {10, 7, 8, 9, 1, 5};
61+
int n = sizeof(nums)/sizeof(nums[0]);
62+
tests(nums, n);
63+
64+
// test 2
65+
int nums2[] = {1, 2, 4, 6, 8, 2, 3, 4, 0, -1, 10, 7, 8, 9, 1, 5};
66+
n = sizeof(nums2)/sizeof(nums2[0]);
67+
tests(nums2, n);
68+
69+
// test 3
70+
int nums3[] = {};
71+
n = sizeof(nums3)/sizeof(nums3[0]);
72+
tests(nums3, n);
73+
74+
// test 4
75+
int nums4[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
76+
n = sizeof(nums4)/sizeof(nums4[0]);
77+
tests(nums4, n);
78+
79+
return 0;
80+
}
81+
```

Data_Struct_Implementation/insertionSort/insertionSort.md

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,96 @@
11
## Insertion Sort
22

3-
#### Code (Linked List Implementation)
3+
![Animation](https://images2017.cnblogs.com/blog/849589/201710/849589-20171015225645277-1151100000.gif)
4+
5+
### **Complexity**
6+
7+
- Auxiliary Space: O(1)
8+
- Best Case: O(n) (reverse sorted)
9+
- Worst Case: O(n^2)
10+
11+
### Code (Array Implementation)
12+
```c
13+
#include <stdlib.h>
14+
#include <stdio.h>
15+
#include <time.h>
16+
17+
typedef void (*sortAlgorithm)(int *array, int size);
18+
19+
void insertionSort(int *array, int size) {
20+
int i, j;
21+
int temp;
22+
23+
for (i = 0; i < size; i++) {
24+
temp = array[i];
25+
for (j = i; j >= 1; j--) {
26+
if (array[j-1] <= temp) {
27+
break;
28+
}
29+
array[j] = array[j-1];
30+
}
31+
array[j] = temp;
32+
}
33+
}
34+
35+
void printArray(int arr[], int size)
36+
{
37+
int i;
38+
for (i=0; i < size; i++)
39+
printf("%d ", arr[i]);
40+
printf("\n");
41+
}
42+
43+
void tests(int *nums, int size) {
44+
sortAlgorithm sort_method = insertionSort;
45+
clock_t start, end;
46+
double cpu_time_used;
47+
48+
printf("==== Sorted array test results ====\n");
49+
printf("Original:\n");
50+
printArray(nums, size);
51+
52+
start = clock();
53+
sort_method(nums, size);
54+
end = clock();
55+
56+
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
57+
58+
printf("Sorted:\n");
59+
printArray(nums, size);
60+
printf("CPU time used: %f\n\n", cpu_time_used);
61+
}
62+
63+
int main() {
64+
// test 1
65+
int nums[] = {10, 7, 8, 9, 1, 5};
66+
int n = sizeof(nums)/sizeof(nums[0]);
67+
tests(nums, n);
68+
69+
// test 2
70+
int nums2[] = {1, 2, 4, 6, 8, 2, 3, 4, 0, -1, 10, 7, 8, 9, 1, 5};
71+
n = sizeof(nums2)/sizeof(nums2[0]);
72+
tests(nums2, n);
73+
74+
// test 3
75+
int nums3[] = {};
76+
n = sizeof(nums3)/sizeof(nums3[0]);
77+
tests(nums3, n);
78+
79+
// test 4
80+
int nums4[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
81+
n = sizeof(nums4)/sizeof(nums4[0]);
82+
tests(nums4, n);
83+
84+
// test 5
85+
int nums5[] = {1, 2, -4, 6, -8, 2, 3, -4, 0, -1, 10, -1, 5};
86+
n = sizeof(nums5)/sizeof(nums5[0]);
87+
tests(nums5, n);
88+
89+
return 0;
90+
}
91+
```
92+
93+
### Code (Linked List Implementation)
494
[Leetcode 147](https://leetcode.com/problems/insertion-sort-list/)
595
```c
696
/**
@@ -11,7 +101,6 @@
11101
* };
12102
*/
13103
14-
15104
struct ListNode* insertionSortList(struct ListNode* head){
16105
struct ListNode *dummy = (struct ListNode *) calloc (1, sizeof(struct ListNode));
17106
@@ -35,8 +124,3 @@ struct ListNode* insertionSortList(struct ListNode* head){
35124
}
36125
```
37126

38-
#### Complexity
39-
40-
- Auxiliary Space: O(1)
41-
- Best Case: O(n) (reverse sorted)
42-
- Worst Case: O(n^2)

0 commit comments

Comments
 (0)