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

C language sort

The document provides an overview of three sorting and searching algorithms: Quick Sort, Linear Search, and Binary Search. Quick Sort is a divide and conquer algorithm with a time complexity of O(n log n) in the best and average cases, and O(n^2) in the worst case. Linear Search is a simple sequential search with a time complexity of O(n), while Binary Search is an efficient algorithm for sorted arrays with a time complexity of O(log n).
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C language sort

The document provides an overview of three sorting and searching algorithms: Quick Sort, Linear Search, and Binary Search. Quick Sort is a divide and conquer algorithm with a time complexity of O(n log n) in the best and average cases, and O(n^2) in the worst case. Linear Search is a simple sequential search with a time complexity of O(n), while Binary Search is an efficient algorithm for sorted arrays with a time complexity of O(log n).
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Quick Sort:
 Like Merge Sort, Quick Sort is a Divide and Conquer algorithm. It picks an element as a
pivot and partitions the given array around the picked pivot.
 There are mainly three steps in the algorithm:
1. Choose a Pivot: Select an element from the array as the pivot. The choice of pivot
can vary (e.g., first element, last element, random element, or median).
2. Partition the Array: Rearrange the array around the pivot. After partitioning, all
elements smaller than the pivot will be on its left, and all elements greater than
the pivot will be on its right. The pivot is then in its correct position, and we obtain
the index of the pivot.
3. Recursively Call: Recursively apply the same process to the two partitioned sub-
arrays (left and right of the pivot).
4. Base Case: The recursion stops when there is only one element left in the sub-
array, as a single element is already sorted.

Algorithm
/* low –> Starting index, high –> Ending index */
quickSort(arr[], low, high) {
if (low < high) {
/* pi is partitioning index, arr[pi] is now at right place */
pi = partition(arr, low, high);
quickSort(arr, low, pi – 1); // Before pi
quickSort(arr, pi + 1, high); // After pi
}
}

JYOTI PRAKASH MOHANTA 1


Algorithm for partition()
partition (arr[], low, high)
{
// pivot (Element to be placed at right position)
pivot = arr[high];
i = (low – 1) // Index of smaller element and indicates the
// right position of pivot found so far
for (j = low; j <= high- 1; j++){
// If current element is smaller than the pivot
if (arr[j] <pivot){
i++; // increment index of smaller element
swap arr[i] and arr[j]
}
}
swap arr[i + 1] and arr[high])
return (i + 1)
}

C Program
#include <stdio.h>
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
int partition(int array[], int low, int high) {
int pivot = array[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (array[j] <= pivot) {
i++;
swap(&array[i], &array[j]);
}
}
swap(&array[i + 1], &array[high]);
return (i + 1);
}

JYOTI PRAKASH MOHANTA 2


void quickSort(int array[], int low, int high) {
if (low < high) {
int pi = partition(array, low, high);
quickSort(array, low, pi - 1);
quickSort(array, pi + 1, high);
}
}

// function to print array elements


void printArray(int array[], int size) {
for (int i = 0; i< size; ++i) {
printf("%d ", array[i]);
}
printf("\n");
}

// main function
int main() {
int data[] = {8, 7, 2, 1, 0, 9, 6};
int n = sizeof(data) / sizeof(data[0]);
printf("Unsorted Array\n");
printArray(data, n);
quickSort(data, 0, n - 1);
printf("Sorted array in ascending order: \n");
printArray(data, n);
}

Time Complexity
 Best Case Complexity - In Quick sort, the best-case occurs when the pivot element is the
middle element or near to the middle element. The best-case time complexity of quick
sort is O(n*logn).
 Average Case Complexity - It occurs when the array elements are in jumbled order that
is not properly ascending and not properly descending. The average case time
complexity of quick sort is O(n*logn).
 Worst Case Complexity - In quick sort, worst case occurs when the pivot element is
either greatest or smallest element. Suppose, if the pivot element is always the last
element of the array, the worst case would occur when the given array is sorted already
in ascending or descending order. The worst-case time complexity of quick sort is O(n2).

JYOTI PRAKASH MOHANTA 3


2. Linear Search:
 Linear Search is defined as a sequential search algorithm that starts at one end and
goes through each element of a list until the desired element is found, otherwise
the search continues till the end of the data set.
 It is the easiest searching algorithm.

Step-1:

Step-2:

Step-3:

Algorithm
 Start from the leftmost element of arr[] and compare Key with each element of arr[]
 If Key matches with an element, return the index.
 If Key doesn’t match with any of the elements, return -1.

JYOTI PRAKASH MOHANTA 4


C Program
#include <stdio.h>
int search(int arr[], int N, int x)
{
int i;
for (i = 0; i< N; i++)
if (arr[i] == x)
return i;
return -1;
}
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int N = sizeof(arr) / sizeof(arr[0]);
int result = search(arr, N, x);
(result == -1)
? printf("Element is not present in array")
: printf("Element is present at index %d", result);
return 0;
}
Output:
Element is present at index 3
Time complexity: O(n)

3. Binary Search:

 Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the
search interval in half of the array.
 The idea of binary search is to use the information that the array is sorted and reduce
the time complexity to O(Log n).

JYOTI PRAKASH MOHANTA 5


Algorithm:
 Divide the search array into two halves by finding the middle index “mid”.
 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.
o If the key is smaller than the middle element, then the left side is used for next
search.
o 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.
C Program
#include <stdio.h>
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
(result == -1)? printf("Element is not present in array")
: printf("Element is present at index %d", result);
return 0;
}
Output:
Element is present at index 3
Time Complexity: O(log n)
JYOTI PRAKASH MOHANTA 6

You might also like