You are on page 1of 11

Borana University

College of Natural and Computational science


Department of Computer Science
DSA Group Assignment

Nó Student name ID. no

1
2
3

4
5
6
7
8

Instr:
Subm. Date:
Yabello, Ethiopia
TABLE OF CONTENTS
INTRODUCTION
1.INSERTION 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
ShellSort 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;
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.

#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++;
}
}
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\n" ;
cin>>n;
cout<<"enter these 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: ";
for( int i = 0 ; i < n; i++){
cout<< arr[i]<<"\t";
}

2.QUICK SORT

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) .

Pseudo Code for recursive QuickSort function :

/* 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
}
}

// C++ Implementation of the Quick Sort Algorithm.


#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++;
}
}
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";
}

SUMMARY
References

You might also like