Skip to content

Commit 6e3c617

Browse files
committed
New sorts
1 parent e5dad3f commit 6e3c617

File tree

3 files changed

+303
-0
lines changed

3 files changed

+303
-0
lines changed

sorting/bitonic_sort.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <stdio.h>
2+
3+
// Function to compare and swap elements
4+
void compareAndSwap(int *x, int *y, int order) {
5+
if ((order == 1 && *x > *y) || (order == 0 && *x < *y)) {
6+
int temp = *x;
7+
*x = *y;
8+
*y = temp;
9+
}
10+
}
11+
12+
// Function to perform bitonic merge
13+
void bitonicMerge(int arr[], int low, int cnt, int order) {
14+
if (cnt > 1) {
15+
int k = cnt / 2;
16+
for (int i = low; i < low + k; i++) {
17+
compareAndSwap(&arr[i], &arr[i + k], order);
18+
}
19+
bitonicMerge(arr, low, k, order); // First half
20+
bitonicMerge(arr, low + k, k, order); // Second half
21+
}
22+
}
23+
24+
// Function to sort a bitonic sequence
25+
void bitonicSort(int arr[], int low, int cnt, int order) {
26+
if (cnt > 1) {
27+
int k = cnt / 2;
28+
// Sort first half in ascending order
29+
bitonicSort(arr, low, k, 1);
30+
// Sort second half in descending order
31+
bitonicSort(arr, low + k, k, 0);
32+
// Merge the whole sequence in order
33+
bitonicMerge(arr, low, cnt, order);
34+
}
35+
}
36+
37+
// Helper function to start the sorting process
38+
void sort(int arr[], int n, int order) {
39+
bitonicSort(arr, 0, n, order);
40+
}
41+
42+
// Function to print the array
43+
void printArray(int arr[], int size) {
44+
for (int i = 0; i < size; i++)
45+
printf("%d ", arr[i]);
46+
printf("\n");
47+
}
48+
49+
// Main function to test Bitonic Sort
50+
int main() {
51+
int arr[] = {12, 4, 7, 9, 6, 3, 8, 10};
52+
int n = sizeof(arr) / sizeof(arr[0]);
53+
54+
printf("Original array: ");
55+
printArray(arr, n);
56+
57+
// Sort the array in ascending order
58+
sort(arr, n, 1);
59+
60+
printf("Sorted array: ");
61+
printArray(arr, n);
62+
63+
return 0;
64+
}

sorting/threeway_merge_sort.c

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
// Function to merge three sorted subarrays
5+
void merge(int arr[], int left, int mid1, int mid2, int right) {
6+
int i, j, k;
7+
int n1 = mid1 - left + 1; // Size of the first subarray
8+
int n2 = mid2 - mid1; // Size of the second subarray
9+
int n3 = right - mid2; // Size of the third subarray
10+
11+
// Temporary arrays
12+
int *L = (int*)malloc(n1 * sizeof(int));
13+
int *M = (int*)malloc(n2 * sizeof(int));
14+
int *R = (int*)malloc(n3 * sizeof(int));
15+
16+
// Copy data to temporary arrays
17+
for (i = 0; i < n1; i++)
18+
L[i] = arr[left + i];
19+
for (j = 0; j < n2; j++)
20+
M[j] = arr[mid1 + 1 + j];
21+
for (k = 0; k < n3; k++)
22+
R[k] = arr[mid2 + 1 + k];
23+
24+
// Merge the temporary arrays back into arr[]
25+
i = 0; j = 0; k = 0;
26+
int idx = left;
27+
28+
while (i < n1 && j < n2 && k < n3) {
29+
if (L[i] <= M[j] && L[i] <= R[k]) {
30+
arr[idx++] = L[i++];
31+
} else if (M[j] <= L[i] && M[j] <= R[k]) {
32+
arr[idx++] = M[j++];
33+
} else {
34+
arr[idx++] = R[k++];
35+
}
36+
}
37+
38+
// Merge remaining elements of L[]
39+
while (i < n1 && j < n2) {
40+
if (L[i] <= M[j]) {
41+
arr[idx++] = L[i++];
42+
} else {
43+
arr[idx++] = M[j++];
44+
}
45+
}
46+
47+
while (j < n2 && k < n3) {
48+
if (M[j] <= R[k]) {
49+
arr[idx++] = M[j++];
50+
} else {
51+
arr[idx++] = R[k++];
52+
}
53+
}
54+
55+
while (i < n1 && k < n3) {
56+
if (L[i] <= R[k]) {
57+
arr[idx++] = L[i++];
58+
} else {
59+
arr[idx++] = R[k++];
60+
}
61+
}
62+
63+
// Copy remaining elements of L[]
64+
while (i < n1) {
65+
arr[idx++] = L[i++];
66+
}
67+
68+
// Copy remaining elements of M[]
69+
while (j < n2) {
70+
arr[idx++] = M[j++];
71+
}
72+
73+
// Copy remaining elements of R[]
74+
while (k < n3) {
75+
arr[idx++] = R[k++];
76+
}
77+
78+
// Free temporary arrays
79+
free(L);
80+
free(M);
81+
free(R);
82+
}
83+
84+
// Recursive function to perform 3-way MergeSort
85+
void mergeSort3Way(int arr[], int left, int right) {
86+
if (left < right) {
87+
int mid1 = left + (right - left) / 3;
88+
int mid2 = mid1 + (right - left) / 3 + 1;
89+
90+
// Recursively sort three subarrays
91+
mergeSort3Way(arr, left, mid1);
92+
mergeSort3Way(arr, mid1 + 1, mid2);
93+
mergeSort3Way(arr, mid2 + 1, right);
94+
95+
// Merge the sorted subarrays
96+
merge(arr, left, mid1, mid2, right);
97+
}
98+
}
99+
100+
// Function to print the array
101+
void printArray(int arr[], int size) {
102+
for (int i = 0; i < size; i++)
103+
printf("%d ", arr[i]);
104+
printf("\n");
105+
}
106+
107+
// Main function to test 3-way MergeSort
108+
int main() {
109+
int arr[] = {12, 4, 7, 9, 6, 3, 8, 10};
110+
int n = sizeof(arr) / sizeof(arr[0]);
111+
112+
printf("Original array: ");
113+
printArray(arr, n);
114+
115+
mergeSort3Way(arr, 0, n - 1); // Sort the array
116+
117+
printf("Sorted array: ");
118+
printArray(arr, n);
119+
120+
return 0;
121+
}

sorting/tim_sort.c

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
#define RUN 32 // Size of the run
6+
7+
// Function to perform insertion sort on a subarray
8+
void insertionSort(int arr[], int left, int right) {
9+
for (int i = left + 1; i <= right; i++) {
10+
int key = arr[i]; // Element to be inserted
11+
int j = i - 1;
12+
13+
// Move elements of arr[left..i-1] that are greater than key
14+
// to one position ahead of their current position
15+
while (j >= left && arr[j] > key) {
16+
arr[j + 1] = arr[j];
17+
j--;
18+
}
19+
arr[j + 1] = key; // Place the key in its correct position
20+
}
21+
}
22+
23+
// Function to merge two sorted subarrays
24+
void merge(int arr[], int left, int mid, int right) {
25+
int i, j, k;
26+
int n1 = mid - left + 1; // Size of left subarray
27+
int n2 = right - mid; // Size of right subarray
28+
29+
// Create temporary arrays for left and right subarrays
30+
int *L = (int*)malloc(n1 * sizeof(int));
31+
int *R = (int*)malloc(n2 * sizeof(int));
32+
33+
// Copy data to temporary arrays L[] and R[]
34+
for (i = 0; i < n1; i++)
35+
L[i] = arr[left + i];
36+
for (j = 0; j < n2; j++)
37+
R[j] = arr[mid + 1 + j];
38+
39+
// Merge the temporary arrays back into arr[left..right]
40+
i = 0;
41+
j = 0;
42+
k = left;
43+
44+
// Merge while both subarrays have elements
45+
while (i < n1 && j < n2) {
46+
if (L[i] <= R[j]) {
47+
arr[k] = L[i]; // If the current element of L[] is smaller
48+
i++;
49+
} else {
50+
arr[k] = R[j]; // If the current element of R[] is smaller
51+
j++;
52+
}
53+
k++;
54+
}
55+
56+
// Copy remaining elements of L[], if any
57+
while (i < n1) {
58+
arr[k] = L[i];
59+
i++;
60+
k++;
61+
}
62+
63+
// Copy remaining elements of R[], if any
64+
while (j < n2) {
65+
arr[k] = R[j];
66+
j++;
67+
k++;
68+
}
69+
70+
// Free the temporary arrays
71+
free(L);
72+
free(R);
73+
}
74+
75+
// Main TimSort function
76+
void timSort(int arr[], int n) {
77+
// Step 1: Sort individual subarrays of size RUN
78+
for (int start = 0; start < n; start += RUN) {
79+
int end = (start + RUN - 1 < n - 1) ? (start + RUN - 1) : (n - 1);
80+
insertionSort(arr, start, end); // Sort the run using insertion sort
81+
}
82+
83+
// Step 2: Merge the sorted runs
84+
for (int size = RUN; size < n; size *= 2) { // Double the size of the run
85+
for (int left = 0; left < n; left += 2 * size) {
86+
int mid = left + size - 1; // Ending index of the left subarray
87+
int right = ((left + 2 * size - 1) < (n - 1)) ? (left + 2 * size - 1) : (n - 1); // Ending index of the right subarray
88+
89+
// Merge the two subarrays if valid
90+
if (mid < right) {
91+
merge(arr, left, mid, right);
92+
}
93+
}
94+
}
95+
}
96+
97+
// Function to print the array
98+
void printArray(int arr[], int size) {
99+
for (int i = 0; i < size; i++)
100+
printf("%d ", arr[i]); // Print each element
101+
printf("\n"); // New line after printing the array
102+
}
103+
104+
// Main function to test the TimSort implementation
105+
int main() {
106+
int arr[] = {5, 21, 7, 23, 19, 10, 1, 34, 0}; // Example array
107+
int n = sizeof(arr) / sizeof(arr[0]); // Calculate number of elements in the array
108+
109+
printf("Original array: ");
110+
printArray(arr, n); // Print the original array
111+
112+
timSort(arr, n); // Call TimSort on the array
113+
114+
printf("Sorted array: ");
115+
printArray(arr, n); // Print the sorted array
116+
117+
return 0; // End of program
118+
}

0 commit comments

Comments
 (0)