Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Java/algorithms/sorting/ShellSortAditya.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
#include <vector>
using namespace std;

// Function to perform Shell Sort
void shellSort(vector<int> &arr) {
int n = arr.size();

// Start with a large gap, then reduce the gap
for (int gap = n / 2; gap > 0; gap /= 2) {
// Do a gapped insertion sort for this gap size
for (int i = gap; i < n; i++) {
int temp = arr[i];
int j;
// Shift earlier gap-sorted elements until the correct location is found
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
arr[j] = arr[j - gap];
}
arr[j] = temp;
}
}
}

int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;

vector<int> arr(n);
cout << "Enter elements: ";
for (int i = 0; i < n; i++)
cin >> arr[i];

shellSort(arr);

cout << "Sorted array: ";
for (int x : arr)
cout << x << " ";
cout << endl;

return 0;
}
Loading