-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added shell sort algorithm in c++,c,java,python
- Loading branch information
Showing
4 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include<iostream> | ||
|
||
using namespace std; | ||
|
||
int shellSort(int array[], int n) | ||
{ | ||
// Start with a big gap, then reduce the gap | ||
for (int gap = n/2; gap > 0; gap /= 2) | ||
{ | ||
// Perform an insertion sort within the current gap size. | ||
// The first "gap" elements, a[0..gap-1], are already sorted with a gap between them. | ||
// Extend the sorted sequence by adding one more element at a time until the entire array is sorted | ||
//with the current gap. | ||
for (int i = gap; i < n; i += 1) | ||
{ | ||
// add a[i] to the elements that have been gap sorted | ||
// save a[i] in temp and make a hole at position i | ||
int temp = array[i]; | ||
|
||
// shift earlier gap-sorted elements up until the correct | ||
// location for a[i] is found | ||
int j; | ||
for (j = i; j >= gap && array[j - gap] > temp; j -= gap) | ||
array[j] = array[j - gap]; | ||
|
||
// put temp (the original a[i]) in its correct location | ||
array[j] = temp; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
void displayArray(int array[], int n) | ||
{ | ||
for (int i=0; i<n; i++) | ||
cout << array[i] << " "; | ||
} | ||
|
||
int main(){ | ||
int array[] = {9,8,1,7,4,3}; | ||
int n = sizeof(array)/sizeof(array[0]); | ||
|
||
cout << "Array before sorting: \n"; | ||
displayArray(array, n); | ||
|
||
shellSort(array, n); | ||
|
||
cout << "\nArray after sorting: \n"; | ||
displayArray(array, n); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <stdio.h> | ||
|
||
void shellSort(int array[], int n) { | ||
// Start with a big gap, then reduce the gap | ||
for (int gap = n/2; gap > 0; gap /= 2) { | ||
// Do a gapped insertion sort for this gap size. | ||
// The first gap elements a[0..gap-1] are already in gapped order. | ||
// Keep adding one more element until the entire array is gap sorted. | ||
for (int i = gap; i < n; i += 1) { | ||
// Add array[i] to the elements that have been gap sorted. | ||
// Save array[i] in temp and make a hole at position i. | ||
int temp = array[i]; | ||
int j; | ||
|
||
// Shift earlier gap-sorted elements up until the correct location for array[i] is found. | ||
for (j = i; j >= gap && array[j - gap] > temp; j -= gap) | ||
array[j] = array[j - gap]; | ||
|
||
// Put temp (the original array[i]) in its correct location. | ||
array[j] = temp; | ||
} | ||
} | ||
} | ||
|
||
void displayArray(int array[], int n) { | ||
for (int i = 0; i < n; i++) | ||
printf("%d ", array[i]); | ||
} | ||
|
||
int main() { | ||
int array[] = {9, 8, 1, 7, 4, 3}; | ||
int n = sizeof(array) / sizeof(array[0]); | ||
|
||
printf("Array before sorting:\n"); | ||
displayArray(array, n); | ||
|
||
shellSort(array, n); | ||
|
||
printf("\nArray after sorting:\n"); | ||
displayArray(array, n); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import java.util.Arrays; | ||
|
||
class shell_sort { | ||
public static void shellSort(int[] arr) { | ||
int n = arr.length; | ||
|
||
// Start with a big gap, then reduce the gap | ||
for (int gap = n / 2; gap > 0; gap /= 2) { | ||
// Do a gapped insertion sort for this gap size. | ||
// The first gap elements a[0..gap-1] are already in gapped order. | ||
// Keep adding one more element until the entire array is gap sorted. | ||
for (int i = gap; i < n; i++) { | ||
int temp = arr[i]; | ||
int j = i; | ||
while (j >= gap && arr[j - gap] > temp) { | ||
// Shift earlier gap-sorted elements up until the correct location for a[i] is found. | ||
arr[j] = arr[j - gap]; | ||
j -= gap; | ||
} | ||
// Put temp (the original a[i]) in its correct location. | ||
arr[j] = temp; | ||
} | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
int[] arr = {9, 8, 1, 7, 4, 3}; | ||
System.out.println("Array before sorting:"); | ||
System.out.println(Arrays.toString(arr)); | ||
shellSort(arr); | ||
System.out.println("Array after sorting:"); | ||
System.out.println(Arrays.toString(arr)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
def shell_sort(arr): | ||
n = len(arr) | ||
|
||
# Start with a big gap, then reduce the gap | ||
gap = n // 2 | ||
while gap > 0: | ||
# Do a gapped insertion sort for this gap size. | ||
# The first gap elements a[0..gap-1] are already in gapped order. | ||
# Keep adding one more element until the entire array is gap sorted. | ||
for i in range(gap, n): | ||
temp = arr[i] | ||
j = i | ||
while j >= gap and arr[j - gap] > temp: | ||
# Shift earlier gap-sorted elements up until the correct location for a[i] is found. | ||
arr[j] = arr[j - gap] | ||
j -= gap | ||
# Put temp (the original a[i]) in its correct location. | ||
arr[j] = temp | ||
gap //= 2 | ||
|
||
return arr | ||
|
||
# Test the algorithm | ||
arr = [9, 8, 1, 7, 4, 3] | ||
print("Array before sorting:") | ||
print(arr) | ||
sorted_arr = shell_sort(arr) | ||
print("Array after sorting:") | ||
print(sorted_arr) |