[go: up one dir, main page]

0% found this document useful (0 votes)
9 views13 pages

Chapter 2

The document discusses elementary searching and sorting algorithms, detailing two searching methods (Linear and Binary Search) and four sorting techniques (Ordinary/Linear Sort, Bubble Sort, Selection Sort, and Insertion Sort). Each algorithm includes a brief explanation, pseudocode, and C++ implementation. The time complexities for the searching algorithms are O(n) for Linear Search and O(log n) for Binary Search, while sorting algorithms are described in terms of their basic operations and methods.

Uploaded by

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

Chapter 2

The document discusses elementary searching and sorting algorithms, detailing two searching methods (Linear and Binary Search) and four sorting techniques (Ordinary/Linear Sort, Bubble Sort, Selection Sort, and Insertion Sort). Each algorithm includes a brief explanation, pseudocode, and C++ implementation. The time complexities for the searching algorithms are O(n) for Linear Search and O(log n) for Binary Search, while sorting algorithms are described in terms of their basic operations and methods.

Uploaded by

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

CHAPTER Two

2. Elementary Sorting and Searching Algorithms

2.1 Searching Algorithms


Searching is a process of looking for a specific element in a list of items or determining that the item is
not in the list. There are two elementary searching algorithms:

• Sequential Search, and


• Binary Search

a) Linear Search (Sequential Search)

Pseudo code

Loop through the array starting at the first element until the value of target matches one of the array
elements.

If a match is not found, return –1.

Time is proportional to the size of input (n) and we call this time complexity O(n).

Algorithm

1 / input “n” elements of the array and elements going to be searched “x”.

2/ initialize i=0 and repeat through step 3 if (i<n)

3/ if (array[i]==x)

flag=1

return i

4/ if (flag==0)

Return -1

5/ if flag = 0 index=-1 otherwise index=i

6/ return index

7/ exit
Implementation:

int Linear_Search(int array[], int x)


{
// take the arrays and element to be searched from calling
//funcitions
int index;
int flag=0;
int i=0;
while(i<n)
{
if(array[i]==x)
{
flag=1;
return i;
}
i++;
}

if(flag==0)
{
index=-1;
}
else
{
index=i;
}
return index;
}
b) Binary Search
This searching algorithms works only on an ordered list.

The basic idea is:

• Locate midpoint of array to search


• Determine if target is in lower half or upper half of an array.
o If in lower half, make this half the array to search
o If in the upper half, make this half the array to search
• Loop back to step 1 until the size of the array to search is one, and this element does not match, in
which case return –1.

The computational time for this algorithm is proportional to log2 n. Therefore the time complexity is
O(log n)

Algorithm

1 / input “n” elements of the array and the element going to be searched “x”;

2/ initialize i=0 and repeat through step 3 if (i<n)

3/ do step “ 4” while left < right and flag =0

4/ mid=(right+left)/2;

if(array[mid]==x)

{flag=1 , return mid }

if(array[mid]<x)

left=mid+1

otherwise

right= mid-1

5/ if flag = 0 index=-1 otherwise index=mid

6/ return index
Implementation:

int Binary_Search(int array[],int x)


{
int left=0;
int right=n-1;
int flag=0;
do{
mid=(right+left)/2;
if(array[mid]==x)
{ flag=1;

else{
if(array[mid]<x)
left=mid+1;
else
right=mid-1;
}
}while(flag==0&&left<right);
if(flag==0)
index=-1;
else
index=mid;
return index;
}

2.2 Sorting Algorithms


Sorting is one of the most important operations performed by computers. Sorting is a process of
reordering a list of items in either increasing or decreasing order.

Sorting is used to arrange names or numbers in meaning full ways.by default sorting is performed in
ascending order.

There are two types of sorting based on the type of memory the sorting technique takes place

1) Internal sorting

=>sorting take places on primary memory or RAM

=> The sorting is temporary.

=> This happen b/c RAM is Volatile.


2) External Sorting

=>sorting takes place on auxiliary or secondary storage device.

=>It is permanent (ROM)

Example in flash Disk, Floppy Disk, Hard Disk

The following are simple sorting algorithms used to sort small-sized lists.

• Ordinary/linear sort
• Bubble Sort
• Selection Sort
• Insertion Sort

a) Ordinary/linear sort

Basic idea:

Hold the first element that is A[0] and compare with all elements of the array. After completing the first
pass the smallest will be found from the array and it can be placed in its right position. Let us consider
“A” is an array with n elements.

Algorithm

1 /input “n” elements of the array;

2/initialize i=0 and repeat through step 4 if (i<n)

3/initialize j=i+1 and repeat through step 4 if (j<n)

4/if(array[i]> array[j])

temp = array[i]

array[i]=array[j]

array[j]=temp

5/Display the sorted element of array

6/exit
Implementation of Ordinary or linear sorting in C++:

#include <iostream.h>
#include <conio.h>
void main ( )
{
clrscr();
int i,j,temp;
int n=5;
int array[5];

for(i=0;i<n; i++){// input n elements for the array.


cout<<”Enter the number”<<i+1;
cin>>a[i];
}

for(int m=0;m<n; m++){// Display n elements before sorting.


cout<<a[m]<<”\t”;
}

for(i=0;i<n; i++){
for(j=i+1;j<n; j++){
if(array[i]>array[j]){
temp=array[i];
array[i]=array[j];
array[j]=temp;
}//swap adjacent elements
}//end of inner loop

}//end of outer loop

for(int k=0;k<n; k++){// Display n elements after sorting.


cout<<a[k]<<”\t”;
}

getch( );

}//end of linear_sort
b) Bubble Sort
Bubble sort is the simplest algorithm to implement and the slowest algorithm on very large inputs.

 Bubble sort:
 Makes a number of passes through array:
 First bubble the largest element putted in last position, by
iteratively comparing & swapping adjacent elements starting
with the start of the list
 Next bubble next largest element putted to last-1 position
 etc. until all apart from first element placed

Basic Idea:

 Loop through array from i=0 to n and swap adjacent elements if they are out of order.

Algorithm

1 /input “n” elements of the array ;

2/initialize i=0 and repeat through step 4 if (i<n)

3/initialize j=0 and repeat through step 4 if (j<n-1-i)

4/if(array[j]>array[j+1])

temp = array[j]

array[j]=array[j+1]

array[j+1]=temp

5/Display the sorted element of array

6/exit
Implementation of Bubble sorting in C++:
#include <iostream.h>
#include <conio.h>
void main ( )
{
clrscr();
int i,j,temp;
int n=5;
int array[5];

for(i=0;i<n; i++){// input n elements for the array.


cout<<”Enter the number”<<i+1;
cin>>a[i];
}

for(int m=0;m<n; m++){// Display n elements before sorting.


cout<<a[m]<<”\t”;
}

for(i=0;i<n; i++){
for(j=0;j<n-1-i; j++){
if(array[j]>array[j+1]){
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}//swap adjacent elements
}//end of inner loop

}//end of outer loop

for(int k=0;k<n; k++){// Display n elements after sorting.


cout<<a[k]<<”\t”;
}

getch( );

}//end of bubble_sort/ main


c) Selection Sort
Each element is swapped directly with the element that occupies its correct position

Basic Idea:
 Scan array to find smallest element: swap it with element in first position
 Scan remainder of array to find smallest element: swap it with element in second
position
 etc. until all elements placed in correct position

Algorithm

1 /input “n” elements of the array ;

2/initialize i=0 and repeat through step 5 if (i<n)

min=array[i]

loc=i

3/initialize j=i+1 and repeat through step 4 if (j<n)

4/if(array[j]<min)

min = array[j]

loc=j

5/if(loc!=i)

temp = array[i]

array[i] = array[loc]

array[loc]=temp

6/Display the sorted element of array 7/exit


Implementation of selection sorting in C++:
#include <iostream.h>
#include <conio.h>
void main ( )
{
clrscr();
int i,j,temp,min,loc;
int n=5;
int array[5];

for(i=0;i<n; i++){// input n elements for the array.


cout<<”Enter the number”<<i+1;
cin>>a[i];
}

for(int m=0;m<n; m++){// Display n elements before sorting.


cout<<a[m]<<”\t”;
}

for(i=0;i<n; i++){
min=array[i];
loc=i;
for(j=i+1;j<n; j++)
{

if(array[j]<min)
{
min = array[j];
loc=j;
}
}//end of inner loop
if(loc != i)
{
temp=array[i];
array[i]=array[loc];
array[loc]=temp;
}
}//end of outer loop

for(int k=0;k<n; k++){// Display n elements after sorting.


cout<<a[k]<<”\t”;
}

getch( );

}//end of selection_sort/ main


d) Insertion Sort
The insertion sort works just like its name suggests - it inserts each item into its proper place in the final
list. The simplest implementation of this requires two list structures - the source list and the list into which
sorted items are inserted. To save memory, most implementations use an in-place sort that works by
moving the current item past the already sorted items and repeatedly swapping it with the preceding item
until it is in place.

It's the most instinctive type of sorting algorithm. The approach is the same approach that you use for
sorting a set of cards in your hand. While playing cards, you pick up a card, start at the beginning of your
hand and find the place to insert the new card, insert it and move all the others up one place.

Basic Idea: Find the location for an element and move all others up, and insert the element.

The process involved in insertion sort is as follows:

 First, consider first two elements: if they are out of order then swap them
 Next consider, the third element: insert it into its correct location in the 2 elements preceding it
 Next consider the fourth element: insert it into its correct location in the 3 elements preceding it
 etc. up to last element.

Algorithm

1 /input “n” elements of the array ;

2/initialize i=1 and repeat through step 4 if (i<n)

temp=array[i]

pos=pos-1

3/ repeat through step “3” if ( temp < array[pos] and pos >=0)

Array[pos+1]=array[pos]

pos=pos-1

4/array[pos+1]=temp

5/Display the sorted element of array

6/exit
Implementation of insertion sorting in C++:
#include <iostream.h>
#include <conio.h>
void main ( )
{
clrscr();
int i,j,temp,pos;
int n=5;
int array[5];

for(i=0;i<n; i++){// input n elements for the array.


cout<<”Enter the number”<<i+1;
cin>>a[i];
}

for(int m=0;m<n; m++){// Display n elements before sorting.


cout<<a[m]<<”\t”;
}

for(i=1;i<n; i++){
temp=array[i];
pos=i-1;
for(;temp<array[pos] && pos>=0;)
{
array[pos+1]=array[pos];
pos=pos-1;

}//end of inner loop


array[pos+1]=temp;
}//end of outer loop

for(int k=0;k<n; k++){// Display n elements after sorting.


cout<<a[k]<<”\t”;
}

getch( );

}//end of insertion_sort/ main


OR

void insertionSort(int arr[], int n)

int i, temp, j;

for (i = 1; i < n; i++) {

temp = arr[i];

j = i - 1;

while (j >= 0 && arr[j] > temp) {

arr[j + 1] = arr[j];

j = j - 1;

arr[j + 1] = temp;

You might also like