mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 05:21:49 +03:00
create shellSort.c
This commit is contained in:
parent
79e04baa43
commit
79e187649d
81
C
Normal file
81
C
Normal file
@ -0,0 +1,81 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void merge(int *array, int low, int middle, int high){
|
||||
int left = middle - low + 1;
|
||||
int right = high - middle;
|
||||
|
||||
int tmpLeft[left], tmpRight[right];
|
||||
|
||||
for(int i = 0; i < left; i++){
|
||||
tmpLeft[i] = array[low + i];
|
||||
}
|
||||
|
||||
for(int j = 0; j < right; j++){
|
||||
tmpRight[j] = array[middle + 1+ j];
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int k = low;
|
||||
|
||||
while(i < left && j < right){
|
||||
if (tmpLeft[i] <= tmpRight[j]){
|
||||
array[k] = tmpLeft[i];
|
||||
i++;
|
||||
}
|
||||
else{
|
||||
array[k] = tmpRight[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
while(i < left){
|
||||
array[k] = tmpLeft[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
while(j < right){
|
||||
array[k] = tmpRight[j];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
void mergeSort(int array[], int low, int high){
|
||||
if (low < high){
|
||||
int middle = (low + high) / 2;
|
||||
mergeSort(array, low, middle);
|
||||
mergeSort(array , middle + 1, high);
|
||||
merge(array, low, middle, high);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int array[] = {10,7,3,9,18,-4,67,32,0,21};
|
||||
int size = sizeof array / sizeof array[0];
|
||||
int high = size - 1;
|
||||
int low = 0;
|
||||
printf("\n");
|
||||
printf("%s", "Original Array: \n");
|
||||
for(int i = 0; i < size; i++) {
|
||||
printf("%d ",array[i]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
printf("\n");
|
||||
printf("%s", "Sorted Array:\n");
|
||||
|
||||
mergeSort(array,low,high);
|
||||
|
||||
for(int i = 0; i < size; i++) {
|
||||
printf("%d ",array[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
48
Sorts/shellSort.c
Normal file
48
Sorts/shellSort.c
Normal file
@ -0,0 +1,48 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
void shellSort(int array[], int value){
|
||||
int i = value;
|
||||
int j, k, tmp;
|
||||
for (i = value / 2; i > 0; i = i / 2){
|
||||
for (j = i; j < value; j++){
|
||||
for(k = j - i; k >= 0; k = k - i){
|
||||
if (array[k+i] >= array[k]){
|
||||
break;
|
||||
}
|
||||
else{
|
||||
tmp = array[k];
|
||||
array[k] = array[k+i];
|
||||
array[k+i] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int array[20];
|
||||
int range = 500;
|
||||
for(int i = 0; i < 100; i++){
|
||||
array[i] = rand() % range + 1;
|
||||
}
|
||||
int size = sizeof array / sizeof array[0];
|
||||
|
||||
|
||||
|
||||
clock_t start = clock();
|
||||
shellSort(array,size);
|
||||
clock_t end = clock();
|
||||
double time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
||||
|
||||
|
||||
printf("Data Sorted\n");
|
||||
printf("%s\n", "Shell Sort Big O Notation:\n--> Best Case: O(n log(n))\n--> Average Case: depends on gap sequence\n--> Worst Case: O(n)\n");
|
||||
printf("Time spent sorting: %f\n", time_spent);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user