0% found this document useful (0 votes)
12 views

Shellsort

The document describes a shell sort algorithm to sort an integer array. The shellSort function takes an integer array and size as parameters and implements the shell sort algorithm by starting with a large gap between elements and reducing the gap by half each iteration to sort the array. The main function calls shellSort to sort a sample integer array, prints it before and after sorting, and returns 0.

Uploaded by

Pawan Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Shellsort

The document describes a shell sort algorithm to sort an integer array. The shellSort function takes an integer array and size as parameters and implements the shell sort algorithm by starting with a large gap between elements and reducing the gap by half each iteration to sort the array. The main function calls shellSort to sort a sample integer array, prints it before and after sorting, and returns 0.

Uploaded by

Pawan Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Program 1

/*Program to implement Shell Sort*/


#include <iostream>
using namespace std;

int shellSort(int arr[], int n) /* function to sort arr using shellSort */


{

// Start with a big gap, then reduce the gap


for (int gap = n/2; gap > 0; gap /= 2)
{
for (int i = gap; i < n; i += 1)
{
int temp = arr[i];
int j;

for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)


arr[j] = arr[j - gap];
arr[j] = temp;
}
}
return 0;
}
void printArray(int arr[], int n)
{
for (int i=0; i<n; i++)
cout << arr[i] << " ";
}

int main()
{
int arr[] = {12, 34, 54, 2, 3}, i;
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Array before sorting: \n";
printArray(arr, n);

shellSort(arr, n);
cout << "\nArray after sorting: \n";
printArray(arr, n);
return 0;
}
Output:

You might also like