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

Searching and Sorting Arrays

Cpp learning

Uploaded by

For Business
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Searching and Sorting Arrays

Cpp learning

Uploaded by

For Business
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Fundamentals of Computer

Programming
(CS-110)
Course Instructors:
Dr. Momina Moetesum
Ms. Shakeela Bibi

1
For Smaller Arrays (<10,000 Elements)

• Bubble Sort
• Selection Sort
• Insertion Sort
Sorting an
Array For Larger Arrays (>10,000 Elements)

• Quick Sort
• Merge Sort
• Radix Sort
• Heap Sort

2
Bubble Sort
• traverse from left and compare
adjacent elements and the higher
one is placed at right side.
• In this way, the largest element is
moved to the rightmost end at
first.
• This process is then continued to
find the second largest and place it
and so on until the data is sorted.

3
Bubble Sort

void bubbleSort(int arr[], int n)


{
int i, j;
for (i = 0; i < n - 1; i++)

// Last i elements are already


// in place
for (j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1])
swap(arr[j], arr[j + 1]);
}

4
Optimized Bubble Sort
void bubbleSort(int arr[], int n) {
bool isUnsorted;
do {
isUnsorted = false;
for (int i = 0; i < (n - 1); i++) {
if (arr[i] > arr[i + 1]) {
isUnsorted = true;
for (; i < (n - 1); i++) {
if (arr[i] > arr[i + 1]) {
std::swap(arr[i], arr[i + 1]);
}
}
}
}
} while (isUnsorted);
}

5
Searching an Array

LINEAR SEARCH BINARY SEARCH


6
Linear Search

Every element is considered as a potential match for the key and checked for
the same.

If any element is found equal to the key, the search is successful and the index
of that element is returned.

If no element is found equal to the key, the search yields “No match found”.

7
Linear Search
// Driver code
int main(void)
// C++ code to linearly search x in arr[]. {
int arr[] = { 2, 3, 4, 10, 40 };
#include <bits/stdc++.h> int x = 10;
using namespace std; int N = sizeof(arr) / sizeof(arr[0]);

int search(int arr[], int N, int x) // Function call


{ int result = search(arr, N, x);
for (int i = 0; i < N; i++) (result == -1)
if (arr[i] == x) ? cout << "Element is not present in array"
return i; : cout << "Element is present at index " << result;
return -1; return 0;
} }

8
Binary Search
• Compare the middle element of the search
space with the key.
• If the key is found at middle element, the
process is terminated.
• If the key is not found at middle element,
choose which half will be used as the next
search space.
• If the key is smaller than the middle
element, then the left side is used for next
search.
• If the key is larger than the middle
element, then the right side is used for
next search.
• This process is continued until the key is found
or the total search space is exhausted.

9
Binary Search Algorithm
1.Start
2.Take input array and Target
3.Initialise start = 0 and end = (array size -1)
4.Intialise mid variable
5.mid = (start+end)/2
6.if array[ mid ] == target then return mid
7.if array[ mid ] < target then start = mid+1
8.if array[ mid ] > target then end = mid-1
9.if start<=end then goto step 5
10.return -1 as Not element found
11.Exit

10
// C++ program to implement iterative Binary Search
#include <iostream>
Iterative Solution using namespace std;

// An iterative binary search function.


int binarySearch(int arr[], int l, int r, int x)
{
// Driver code while (l <= r) {
int main(void) int m = l + (r - l) / 2;
{ // Check if x is present at mid
const int n=5; if (arr[m] == x)
int arr[n] = { 2, 3, 4, 10, 40 }; return m;
int x = 10; // If x greater, ignore left half
int result = binarySearch(arr, 0, n - 1, x); if (arr[m] < x)
if (result == -1) l = m + 1;
cout << "Element is not present in array“; // If x is smaller, ignore right half
else else
cout << "Element is present at index " << result; r = m - 1;
return 0; }
} // If we reach here, then element was not present
return -1;
}
11
Recursive // C++ program to implement recursive Binary Search
#include <iostream>
Solution using namespace std;
// A recursive binary search function. It returns
// location of x in given array arr[l..r] is present otherwise -1
int binarySearch(int arr[], int l, int r, int x)
{
// Driver code if (r >= l) {
int main(void) int mid = l + (r - l) / 2;
{ // If the element is present at the middle itself
const int n=5; if (arr[mid] == x)
int arr[n] = { 2, 3, 4, 10, 40 }; return mid;
int x = 10; // If element is smaller than mid, then
int result = binarySearch(arr, 0, n - 1, x); // it can only be present in left subarray
if (result == -1) if (arr[mid] > x)
cout << "Element is not present in array“; return binarySearch(arr, l, mid - 1, x);
else // Else the element can only be present in right subarray
cout << "Element is present at index " << return binarySearch(arr, mid + 1, r, x);
result; }
return 0; // We reach here when element is not present in array
} return -1;
} 12
Acknowledgment

• Content of these slides are taken from:


• https://www.geeksforgeeks.org/
• https://www.tutorialspoint.com/
• https://www.programiz.com/
• https://www.w3schools.com/

13

You might also like