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

Analysis of Algorithms: Sorting

The document describes three sorting algorithms: bubble sort, insertion sort, and selection sort. It provides pseudocode for the algorithms and C++ implementations. It also describes binary search and provides recursive and iterative pseudocode implementations.

Uploaded by

Mohsin Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Analysis of Algorithms: Sorting

The document describes three sorting algorithms: bubble sort, insertion sort, and selection sort. It provides pseudocode for the algorithms and C++ implementations. It also describes binary search and provides recursive and iterative pseudocode implementations.

Uploaded by

Mohsin Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Analysis of Algorithms

Sorting

BUBBLE SORT
BUBBLESORT [A]
for i 1 to length [A]
do
for j length [A] downto i +1
do
If A[A] < A[j-1] then
Exchange A[j] A[j-1]

Implementation
void bubbleSort(int numbers[], int array_size)
{
int i, j, temp;
for (i = (array_size - 1); i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (numbers[j-1] > numbers[j])
{
temp = numbers[j-1];
numbers[j-1] = numbers[j];
numbers[j] = temp;
}

INSERTION SORT
INSERTION_SORT [A]
1. FOR j 2 TO length[A]
2.
DO key A[j]
3
{ Put A[j] into the sorted sequence A[1 . . j 1] }
4.
ij1
5.
WHILE i > 0 and A[i] > key
6.
DO A[i +1] A[i]
7.
ii1
8.
A[i + 1] key

Implementation
void insertionSort(int numbers[], int array_size)
{
int i, j, index;

for (i = 1; i < array_size; i++)


{
index = numbers[i];
j = i;
while ((j > 0) && (numbers[j 1] > index))
{
numbers[j] = numbers[j 1];
j = j 1;
}
numbers[j] = index;
}

SELECTION SORT
SELECTION_SORT [A]
for i 1 to n-1 do
min j i;
min x A[i]
for j i + 1 to n do
If A[j] < min x then
min j j
min x A[j]
A[min j] A [i]
A[i] min x

Implementation
void selectionSort(int numbers[], int array_size)
{
int i, j;
int min, temp;
for (i = 0; i < array_size-1; i++)
{
min = i;
for (j = i+1; j < array_size; j++)
{
if (numbers[j] < numbers[min])
min = j;
}
temp = numbers[i];
numbers[i] = numbers[min];
numbers[min] = temp;
}
}

Binary Search
int Binary_Search(list, target)
{
int mid;
int first = 0;
int last = list.length( ) -1;
while ( first <= last )
{
mid = (first + last) / 2;
if ( list[mid] == target )
return mid;
if ( list[mid] > target )
last = mid - 1;
else
first = mid + 1;
}
return -1;
}

Binary Search
int Binary_Search (list, target, int first, int last
{
if (first > last)
return -1;
int mid = (first + last) / 2;
if (list[mid] == target)
return mid;
if (list[mid] < target)
return Binary_Search(list, target, mid+1, last);
else
return Binary_Search(list, target, first, mid-1);

You might also like