Professional Documents
Culture Documents
Group Ass cc++DSA
Group Ass cc++DSA
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
#include<iostream>
using namespace std;
}
return j-1;
}
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 :
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";
}
}