0% found this document useful (0 votes)
6 views5 pages

Unit 2

Uploaded by

renukabhasme03
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)
6 views5 pages

Unit 2

Uploaded by

renukabhasme03
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/ 5

Searching and Sorting

2.1 Searching
Searching: The process of finding a specific item in a data set.

. Two primary methods of searching are Linear Search and Binary Search.

(i) Linear Search: A method where each element in a list is checked


one by one until the desired element is found or the end of
the list is reached. It is straightforward but can be
inefficient for large lists.

Pseudocode:

Algorithm LinearSearch(A, n, key)


// A is the array where elements are stored
// n is the total number of elements in the array A
// key is the element to be searched in array A

1. Set i = 0
2. Repeat steps 3 and 4 while i < n
3. If A[i] == key, then
return i // Element found at index i
4. Set i = i + 1
5. Return -1 // Element not found in the array

(ii) Binary Search


A more efficient method that only works on sorted lists. It involves
repeatedly dividing the list in half to locate the desired element by
comparing it with the middle item.

Pseudocode:

Algorithm BinarySearch(A, n, key)


// A is the sorted array where elements are stored
// n is the total number of elements in the array A
// key is the element to be searched in array A

1. Set low = 0
2. Set high = n - 1
3. Repeat steps 4 and 5 while low <= high
4. Set mid = (low + high) / 2
5. If A[mid] == key, then
return mid // Element found at index mid
Else if A[mid] < key, then
low = mid + 1 // Search in the right half
Else
high = mid - 1 // Search in the left half
6. Return -1 // Element not found in the array

2.2 Sorting
The process of arranging items in a specific order (e.g., ascending or
descending).

(i) Bubble Sort


A simple sorting algorithm that repeatedly steps through the list, compares adjacent
elements, and swaps them if they are in the wrong order. (i.e first element is greater
than the second like it for other numbers)

Pseudocode:

Algorithm BubbleSort(A, n)
// A is the array to be sorted
// n is the total number of elements in the array A

1. Repeat steps 2 and 3 for i from 0 to n - 1


2. Set swapped = false
3. Repeat steps 4 and 5 for j from 0 to n - i - 2
4. If A[j] > A[j + 1], then
Swap A[j] and A[j + 1]
Set swapped = true
6. If swapped is false, then
Break // No swaps means the array is sorted

(ii) Selection Sort


A sorting technique that divides the list into a sorted and an unsorted part. It
repeatedly selects the smallest (or largest) element from the unsorted part and moves it
to the end of the sorted part.

Pseudocode:
Algorithm SelectionSort(A, n)
// A is the array to be sorted
// n is the total number of elements in the array A

1. Repeat steps 2 to 5 for i from 0 to n - 2


2. Set minIndex = i
3. Repeat step 4 for j from i + 1 to n - 1
4. If A[j] < A[minIndex], then
Set minIndex = j
5. If minIndex != i, then
Swap A[i] and A[minIndex]

(iii) Insertion Sort

A sorting algorithm that builds a sorted list one item at a time by


taking each new item and inserting it into its correct position within
the already sorted part.

Pseudocode:

Algorithm InsertionSort(A, n)
// A is the array to be sorted
// n is the total number of elements in the array A

1. Repeat steps 2 to 5 for i from 1 to n - 1


2. Set key = A[i]
3. Set j = i - 1
4. While j >= 0 and A[j] > key
A[j + 1] = A[j]
j=j-1
5. Set A[j + 1] = key

(iv) Quick Sort


A divide-and-conquer sorting algorithm that selects a “pivot”
element and partitions the list into two sub-lists: one with elements
less than the pivot and the other with elements greater than the
pivot, recursively sorting the sub-lists.

Pseudocode:

Algorithm QuickSort(A, low, high)


// A is the array to be sorted
// low is the starting index
// high is the ending index

1. If low < high, then


2. Set pivotIndex = Partition(A, low, high)
3. Call QuickSort(A, low, pivotIndex - 1)
4. Call QuickSort(A, pivotIndex + 1, high)

Algorithm Partition(A, low, high)


1. Set pivot = A[high]
2. Set i = low - 1
3. Repeat steps 4 and 5 for j from low to high - 1
4. If A[j] <= pivot, then
i=i+1
Swap A[i] and A[j]
5. Swap A[i + 1] and A[high]
6. Return i + 1

(v) Radix Sort


A non-comparative sorting algorithm that sorts numbers by
processing individual digits, starting from the least significant digit
to the most significant digit.

Pseudocode:

Algorithm RadixSort(A, n)
// A is the array to be sorted
// n is the total number of elements in the array A

1. Set max = FindMaximum(A)


2. For exp from 1 to max digit count
3. Call CountingSort(A, n, exp)

Algorithm CountingSort(A, n, exp)


1. Set output[n] // output array
2. Set count[10] = {0} // Initialize count array to zero
3. Repeat steps 4 to 6 for i from 0 to n - 1
4. Set index = (A[i] / exp) % 10 // Find the digit at exp place
5. Increment count[index] by 1 // Count occurrences of each digit
6. Repeat steps 7 to 8 for i from 1 to 9
7. Set count[i] = count[i] + count[i - 1] // Update count[i] to be cumulative
8. Repeat steps 9 to 11 for i from n - 1 down to 0
9. Set index = (A[i] / exp) % 10
10. Set output[count[index] - 1] = A[i] // Place in output array
11. Decrement count[index] by 1
12. Repeat step 13 for i from 0 to n - 1
13. Set A[i] = output[i] // Copy output array back to A

You might also like