Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

BORANA UNIVERSITY

COLLEGE OF NATURAL AND COMPUTATIONAL SCIENCE


Department of Computer Science

DSA GROUP ASSIGNMENT

NO STUDANT NAME ID NO
1
2
3
4
5
6
7

INSTRUCTOR:
SUBMISSION DATE

YABELO,ETHIOPIA
ASSIGNMEN
DATA STRUCTURE AND ALGORITHM
T

SHELL SORT

Shell sort is mainly a variation of Insertion Sort. In insertion sort, we move elements
only one position ahead. When an element has to be moved far ahead, many
movements are involved. The idea of Shell Sort is to allow the exchange of far items.
In Shell sort, we make the array h-sorted for a large value of h. We keep reducing
the value of h until it becomes 1.
 An array is said to be h-sorted if all sublists of every h’th element are sorted.

Algorithm:

Step 1 − Start
Step 2 − Initialize the value of gap size. Example: h
Step 3 − Divide the list into smaller sub-part. Each must have equal
intervals to h
Step 4 − Sort these sub-lists using insertion sort
Step 5 – Repeat this step 2 until the list is sorted.
Step 6 – Print a sorted list.
Step 7 – Stop.

PSEUDOCODE :

procedure shell_sort(array, n)
while gap < length(array) /3 :
gap = ( interval * 3 ) + 1
end while loop
while gap > 0 :
for (outer = gap; outer < length(array); outer++):
insertion_value = array[outer]
inner = outer;
ASSIGNMEN
DATA STRUCTURE AND ALGORITHM
T

while inner > gap-1 and array[inner – gap] >= insertion_value:


array[inner] = array[inner – gap]
inner = inner – gap
end while loop
array[inner] = insertion_value
end for loop
gap = (gap -1) /3;
end while loop
end shell_sort

Following is the implementation of ShellSort in C++.

#include<iostream>
using namespace std;

void swap(int arr[] , int pos1, int pos2){


int temp;
temp = arr[pos1];
arr[pos1] = arr[pos2];
arr[pos2] = temp;
}

int partition(int arr[], int low, int high, int pivot){


int i = low;
int j = low;
while( i <= high){
if(arr[i] > pivot){
i++;
}
else{
swap(arr,i,j);
i++;
j++;
}
ASSIGNMEN
DATA STRUCTURE AND ALGORITHM
T

}
return j-1;
}

void quickSort(int arr[], int low, int high){


if(low < high){
int pivot = arr[high];
int pos = partition(arr, low, high, pivot);

quickSort(arr, low, pos-1);


quickSort(arr, pos+1, high);
}
}

int main()
{
int n ;
cout << " enter the size of array";
cin>>n;
int arr[n];
for( int i = 0 ; i < n; i++){
cin>> arr[i];
}
quickSort(arr, 0 , n-1);
cout<<"The sorted array is: ";
for( int i = 0 ; i < n; i++){
cout<< arr[i]<<"\t";
}

QUICK SORT
ASSIGNMEN
DATA STRUCTURE AND ALGORITHM
T

The Quick Sort algorithm is a Divide and Conquer algorithm. It initially selects an
element as a pivot element and partitions the given array around the picked pivot.
 There are many different versions of quickSort that pick pivot in different ways.
Always pick the first element as a pivot (implemented below).
Always pick the last element as the pivot.
Pick a random element as a pivot.
Pick median as a pivot.
The key process in quickSort is the partition() process. The aim of the partition()
function is to receive an array and an element x of the array as a pivot, put x at its
correct position in a sorted array and then put all smaller elements (smaller than x)
before x, and put all greater elements (greater than x) after x.
 All this should be done in linear time i.e. Big O(n) .

PSEUDOCODE :

/* low --> Starting index, high --> Ending index */


quickSort(arr[], low, high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
pi = partition(arr, low, high);

quickSort(arr, low, pi - 1); // Before pi


quickSort(arr, pi + 1, high); // After pi
}
}

Following is the implementation of ShellSort in C++.

// C++ Implementation of the Quick Sort Algorithm


#include<iostream>
ASSIGNMEN
DATA STRUCTURE AND ALGORITHM
T

using namespace std;

void swap(int arr[] , int pos1, int pos2){


int temp;
temp = arr[pos1];
arr[pos1] = arr[pos2];
arr[pos2] = temp;
}

int partition(int arr[], int low, int high, int pivot){


int i = low;
int j = low;
while( i <= high){
if(arr[i] > pivot){
i++;
}
else{
swap(arr,i,j);
i++;
j++;
}
}
return j-1;
}

void quickSort(int arr[], int low, int high){


if(low < high){
int pivot = arr[high];
int pos = partition(arr, low, high, pivot);

quickSort(arr, low, pos-1);


quickSort(arr, pos+1, high);
}
}
ASSIGNMEN
DATA STRUCTURE AND ALGORITHM
T

int main()
{
int n ;
cout << " Enter the size of array:\n";
cin>>n;
cout << " Enter elements of array:\n";
int arr[n];
for( int i = 0 ; i < n; i++){
cin>> arr[i];
}
quickSort(arr, 0 , n-1);
cout<<"The sorted array is: \n ";
for( int i = 0 ; i < n; i++){
cout<< arr[i]<<"\t";
}
}

You might also like