Ch5 Searching Sorting Algorithm
Ch5 Searching Sorting Algorithm
Ch5 Searching Sorting Algorithm
SORTING ALGORITHMS
5
Chapter 5
ARRAYS
Section 5.1
An array
• is a collection of variables of the same type that are
referenced by a common name.
• Declaraction syntax:
Consists of elements sample[0],
sample[1], sample[2]... sample[9]
• Type var_name[size];
• Example:
• int sample[10];
# of rows # of columns
• int sample[3][3];
0 1 2
0 [0][0] [0][1] [0][2]
1 [1][0] [1][1] [1][2]
2 [2][0] [2][1] [2][2]
Sample program on two-dimensional array
#include <iostream>
int main()
{
int i, j;
int x[3][3]={{10,25,33}, {21,32,43},{20,42,51}};
cout<<"\n3x3 arrays' subscripts and\n“;
cout<<"their respective elements\n“;
Cout<<"--------------------------\n“;
// the outer for loop, reading the row by row...
for(i=0; i<3; i++) //i=1
// the inner loop, for every row, read every column by column...
for(j=0; j<3; j++) //j=0
cout<<"[“<<i<<“]“<<“[“<<j<<“]”<<“=“<<x[i][j]; //[0][0]= 10 [0][1]=25
[0][2]=33
return 0;
}
Multi-dimensional array
• data_type array_name[size1][size2]…[sizeN];
• Example:
• int y[4][5][3];
• }
• //find the max value and min value
• int i,j,temp;
• //sort array to find max and
min values
• for(i=0;i<5;i++)
• for(j=4;j>i;j--)
• if(arr[j]<arr[j-1]) {
BUBBLE SORT
Section 5.2
11
Bubble Sort
How Does the C Program for Bubble Sort Work?
• The program for bubble sort works by comparing and swapping adjacent
elements in an array. Let’s understand this in a step-by-step method:
• Suppose we want to sort an array, let’s name it arr, with n elements in
ascending order; this is how the bubble sort algorithm will work.
1. Starts from the first index: arr[0] and compares the first and second
element: arr[0] and arr[1]
2. If arr[0] is greater than arr[1], they are swapped
3. Similarly, if arr[1] is greater than arr[2], they are swapped
4. The above process continues until the last element arr[n-1]
• All four steps are repeated for each iteration. Upon completing each
iteration, the largest unsorted element is moved to the end of the array.
Finally, the program ends when no elements require swapping, giving us the
array in ascending order.
Sample Program
/#include <iostream> bubble sort: ";
int main(){ for(x = 0; x < num; x++){
int arr[50], num, x, y, temp; cout<< arr[x];
cout<<"Please Enter the Number of }
Elements you want in the array: "; return 0;
cin>>num; }
cout<<"Please Enter the Value of
Elements: ";
for(x = 0; x < num; x++)
cin>>arr[x];
for(x = 0; x < num - 1; x++){
for(y = 0; y < num - x - 1; y++)
{
if(arr[y] > arr[y + 1]){
temp = arr[y];
arr[y] = arr[y + 1];
arr[y + 1] = temp;
}
}
}
cout<<"Array after implementing
C Program for Bubble Sort Using Functions
#include <iostream> }
arr[y + 1] = temp;
}
C Program for Bubble Sort Using Functions
cout<<"Please Enter the Value of
Elements: ";
cin>>arr[x];
bubbleSortExample(arr, n);
cout<< arr[x];
return 0;
}
INSERTION SORT
Section 5.3
16
Insertion Sort
Insertion sort algorithm is a basic sorting algorithm that sequentially
sorts each item in the final sorted array or list. It is significantly low
on efficiency while working on comparatively larger data sets. While
other algorithms such as quicksort, heapsort, or merge sort have
time and again proven to be far more effective and efficient.
To sort an array in ascending order, you will follow the steps below:
• k = k - 1;
• int i, key, k;
• }
• for (i = 1; i < size; i++)
• array[k + 1] = key;
• {
• }
• key = array[i];
C Program for Insertion Sort
}
} {
// A function to print the array of size
n int arr[] = { 6, 5, 4, 2, 3 };
} return 0;
/* Driver code */ }
int main()
LINEAR SEARCH
Section 3.2
21
Linear Search
• In computer science, linear search or sequential
search is a method for finding an element within a list. It
sequentially checks each element of the list until a match
is found or the whole list has been searched.
• Makes at most n comparisons, where n is the length of
the list.
• Linear search is rarely practical because other search
algorithms and schemes, such as the binary search
algorithm allow significantly faster searching for all but
short lists.
Linear Search Approach
• Start from the leftmost element of arr[] and one by one
compare x with each element of arr[]
• If x matches with an element, return the index.
• If x doesn’t match with any of elements, return -1.
23
Binary Search
• Search a sorted array by repeatedly dividing the search
interval in half. Begin with an interval covering the whole
array. If the value of the search key is less than the item in
the middle of the interval, narrow the interval to the lower
half. Otherwise narrow it to the upper half. Repeatedly
check until the value is found or the interval is empty.
28
Example
1. Compare x with the middle element.
2. If x matches with middle element, we return the mid index.
3. Else If x is greater than the mid element, then x can only lie
in right half subarray after the mid element. So we recur for
right half.
4. Else (x is smaller) recur for the left half.
29
Drill
• Find the maximum element in an array using:
(a) Linear Search
(b) Binary Search
Drill 1
• Examples :
• Input: arr[] = {8, 10, 20, 80, 100, 200, 400, 500, 3, 2, 1}
• Output: 500