Some Sorting and Searching Algorithms

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Some Sorting and Searching Algorithms

Sorting Problem: Given sequence of n numbers {a1, a2, , an}, reorder them into a sequence {a1, a2, , an}, such that {a1 a2 an}. Some simple sorting algorithms are:
1. Bubble Sort 2. Insertion Sort 3. Selection Sort

Bubble Sort: The Bubble Sort algorithm proceeds through a sequence of iterations, each time swapping adjacent elements that are out of order.
void bubblesort(float a[], int n) { for(int i = 1; i < n; i++) for(int j = 0; j < n - i; j++) if(a[j] > a[j+1]) swap(a[j], a[j+1]); }

void bubblesort(float a[], int n) { for(int i = 1; i < n; i++) for(int j = 0; j < n - i; j++) if(a[j] > a[j+1]) swap(a[j], a[j+1]); } void swap(float & a, float & b) {float temp = a; a = b; b = temp;} int main() { float a[20]; int n, i; cout << "Enter the number of elements " <<endl; cin >> n; cout << "Enter the numbers to be sorted" << endl; for(i=0; i< n; i++) cin >> a[i]; bubblesort(a, n); cout << "The numbers in sorted order are" <<endl; for(i=0; i< n; i++) cout << a[i] << endl; }

Insertion Sort: The Insertion Sort algorithm starts with a single element which is trivially sorted. Then incrementally inserts the remaining elements so that the list stays sorted.
void insertionsort(float a[], int n) { for(int i = 0; i< n; i++) {int j = i; while(j > 0 && (a[j] < a[j-1])) {swap(a[j-1], a[j]); j = j-1; } } }

void insertionsort(float a[], int n) {for(int i = 0; i< n; i++) {int j = i; while(j > 0 && (a[j] < a[j-1])) {swap(a[j-1], a[j]); j = j-1;} } } void swap(float & a, float & b) {float temp = a; a = b; b = temp; } int main() { float a[20]; int n, i; cout << "Enter the number of elements" <<endl; cin >> n; cout << "Enter the numbers to be sorted" << endl; for(i=0; i< n; i++) cin >> a[i]; insertionsort(a, n); cout << "The numbers in sorted order are" <<endl; for(i=0; i< n; i++) cout << a[i] << endl; }

Selection Sort: The Selection Sort algorithm works on the following principle: From those numbers that are currently unsorted, find the smallest and place it next to the sorted list. Thus it first finds the smallest element of the array a and exchange it with a[0], then finds the second smallest element of a and exchange it with a[1], and so on.
void selectionsort(float a[], int n) { for(int i = 0; i< n; i++) {int j = i; for(int k = i+1; k < n; k++) if(a[k] < a[j]) j = k; swap(a[i], a[j]); } }

void selectionsort(float a[], { for(int i = 0; i < n; i++) {int j = i; for(int k = i+1; k < n; if(a[k] < a[j]) j = k; swap(a[i], a[j]); } } void swap(float & a, float & {float temp = a; a = b; b

int n)

k++)

b) = temp;

int main() { float a[20]; int n, i; cout << "Enter the number of elements" << endl; cin >> n; cout << "Enter the numbers to be sorted" << endl; for(i=0; i< n; i++) cin >> a[i]; selectionsort(a, n); cout << "The numbers in sorted order are" << endl; for(i=0; i< n; i++) cout << a[i] << endl; }

Searching Problem: Searching for an element in a sorted array. Given an array {a1, a2, , an} sorted in a non-decreasing order, and a number x. Find an index i such that x = a[i] or return a special value if x is not in the array. Two simple searching algorithms are:
1. Linear search algorithm. 2. Binary search algorithm.

Linear Search Algorithm: The Linear Search algorithm starts at the beginning and inspects each element, one after the other, until the object is found.
int linsearch(int a[], int x, int n) { for(int i=0; i < n; i++)

if(a[i] == x) return i;
return -1; } // x not found.

int linsearch(int a[], int x, int n) { for(int i=0; i<n; i++) if(a[i] == x) return i; return -1; // x not found. } int main() { int a[20], n, x; cout << "How many elements in the array?" <<endl; cin >> n; cout << "Enter the sorted array" << endl; for(int i=0; i< n; i++) cin >> a[i]; cout << "Enter the number to be searched" << endl; cin >> x; int y = linsearch(a, x, n); if(y == (-1)) cout << "The number is not found in the given array " << endl; else cout << "The number searched has index " << y <<endl; }

Binary Search Algorithm: The Binary Search algorithm uses the divide-and-conquer strategy. It repeatedly divides the array into two (equal) pieces and then search the piece that could contain x. It works as follows: If x is equal to the middle element of the array a, then return the index of the middle element. Else if x is greater than the middle element, search in the right subarray, else search in the left subarray. Return a special value if x is not found in the array.

int binsearch(int a[], int x, int n) { int left = 0, right = n-1; while(left <= right) { int mid = (left + right)/2; if(x < a[mid]) right = mid - 1; else if(x > a[mid]) left = mid + 1; else return mid; } return -1; // x not found. }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy