Skip to content

Commit

Permalink
added shell sort algorithm in c++,c,java,python
Browse files Browse the repository at this point in the history
  • Loading branch information
ANI2707 committed Jun 3, 2023
1 parent 3143f76 commit ea43e96
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 0 deletions.
51 changes: 51 additions & 0 deletions C++/Sorting/shell_sort.cpp
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;
}
43 changes: 43 additions & 0 deletions C/Sorting/shell_sort.c
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;
}
34 changes: 34 additions & 0 deletions Java/Sorting/shell_sort.java
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));
}
}
29 changes: 29 additions & 0 deletions Python/Sorting/shell_sort.py
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)

0 comments on commit ea43e96

Please sign in to comment.