Array Operations: Searching and Sorting
- Complete Lecture Notes
These notes cover array searching and sorting techniques in detail, including theory,
algorithms, flow of execution, complexity analysis, Java programs, dry runs,
comparison tables, viva questions, and MCQs.
Introduction to Arrays
Array is a collection of elements of the same data type stored in contiguous memory
locations.
Advantages: Fast access using index, easy traversal.
Disadvantages: Fixed size, insertion/deletion may be costly.
Searching in Arrays
Searching is the process of finding the location of a desired element in an array.
Linear Search
Linear Search checks each element one by one until the target is found.
Binary Search
Binary Search works only on sorted arrays and repeatedly divides the search space.
Sorting in Arrays
Sorting arranges elements in ascending or descending order.
Bubble Sort
Repeatedly swaps adjacent elements if they are in the wrong order.
Selection Sort
Finds the minimum element and places it at the correct position.
Insertion Sort
Builds a sorted portion by inserting elements into their correct positions.
Quick Sort
A divide-and-conquer algorithm that uses a pivot element.
Merge Sort
A divide-and-conquer algorithm that recursively divides and merges arrays.
Stepwise Algorithms
Linear Search
Step 1: Start
Step 2: Read array and key
Step 3: Compare each element with key
Step 4: If found return index
Step 5: Else continue
Step 6: If end reached report not found
Step 7: Stop
JAVA CODE:
import [Link].*;
class Lsearch{
public static int search(int arr[], int N, int x)
// Iterate over the array in order to
// find the key x
for (int i = 0; i < N; i++) {
if (arr[i] == x)
return i;
return -1;
public static void main(String args[])
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int result = search(arr, [Link], x);
if (result == -1)
[Link](
"Element is not present in array");
else
[Link]("Element is present at index "
+ result);
}
Binary Search
Step 1: Start
Step 2: Set low and high
Step 3: Find mid
Step 4: Compare key with mid element
Step 5: Update low/high
Step 6: Repeat until found or range ends
Step 7: Stop
Java Code:
class GFG {
static int binarySearch(int arr[], int x) {
int low = 0, high = [Link] - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
// Check if x is present at mid
if (arr[mid] == x)
return mid;
// If x greater, ignore left half
if (arr[mid] < x)
low = mid + 1;
// If x is smaller, ignore right half
else
high = mid - 1;
// If we reach here, then element was
// not present
return -1;
public static void main(String args[]) {
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int result = binarySearch(arr, x);
if (result == -1)
[Link](
"Element is not present in array");
else
[Link]("Element is present at "
+ "index " + result);
Bubble Sort
Step 1: Perform passes
Step 2: Compare adjacent elements
Step 3: Swap if required
Step 4: Repeat until sorted
Selection Sort
Step 1: Select minimum element
Step 2: Swap with current position
Step 3: Repeat
Insertion Sort
Step 1: Pick next element
Step 2: Shift larger elements
Step 3: Insert at correct position
Quick Sort
Step 1: Choose pivot
Step 2: Partition array
Step 3: Recursively sort left and right parts
Merge Sort
Step 1: Divide array
Step 2: Recursively sort halves
Step 3: Merge sorted halves
Complexity Analysis
Algorithm Best Worst Stable
Linear Search O(1) O(n) Yes
Binary Search O(1) O(log n) Yes
Bubble Sort O(n) O(n²) Yes
Selection Sort O(n²) O(n²) No
Insertion Sort O(n) O(n²) Yes
Quick Sort O(n log n) O(n²) No
Merge Sort O(n log n) O(n log n) Yes
Java Programs
Linear Search
public static int linearSearch(int arr[], int key){
for(int i=0;i<[Link];i++) if(arr[i]==key) return i;
return -1;
}
Binary Search
public static int binarySearch(int arr[], int key){
int low=0, high=[Link]-1;
while(low<=high){
int mid=(low+high)/2;
if(arr[mid]==key) return mid;
if(arr[mid]<key) low=mid+1; else high=mid-1;
}
return -1;
}
Bubble Sort
for(int i=0;i<n-1;i++)
for(int j=0;j<n-i-1;j++)
if(arr[j]>arr[j+1]){
int t=arr[j]; arr[j]=arr[j+1]; arr[j+1]=t;
}
Exam Note 1
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 2
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 3
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 4
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 5
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 6
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 7
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 8
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 9
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 10
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 11
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 12
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 13
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 14
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 15
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 16
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 17
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 18
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 19
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 20
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 21
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 22
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 23
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 24
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 25
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 26
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 27
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 28
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 29
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Exam Note 30
Searching and sorting are fundamental operations in data structures. Understanding their
working, complexity, advantages, disadvantages, and applications is essential for
programming interviews and university examinations. This extended explanation is
included to provide detailed study material.
Viva Questions
What is an array?
What is searching?
Difference between linear and binary search?
Why does binary search require a sorted array?
What is a stable sorting algorithm?
What is the pivot in quick sort?
Explain merge sort.
MCQs
1. Which sorting algorithm uses a pivot element?
A) Bubble B) Selection C) Quick D) Insertion
Answer: C
2. Which sorting algorithm uses a pivot element?
A) Bubble B) Selection C) Quick D) Insertion
Answer: C
3. Which sorting algorithm uses a pivot element?
A) Bubble B) Selection C) Quick D) Insertion
Answer: C
4. Which sorting algorithm uses a pivot element?
A) Bubble B) Selection C) Quick D) Insertion
Answer: C
5. Which sorting algorithm uses a pivot element?
A) Bubble B) Selection C) Quick D) Insertion
Answer: C
6. Which sorting algorithm uses a pivot element?
A) Bubble B) Selection C) Quick D) Insertion
Answer: C
7. Which sorting algorithm uses a pivot element?
A) Bubble B) Selection C) Quick D) Insertion
Answer: C
8. Which sorting algorithm uses a pivot element?
A) Bubble B) Selection C) Quick D) Insertion
Answer: C
9. Which sorting algorithm uses a pivot element?
A) Bubble B) Selection C) Quick D) Insertion
Answer: C
10. Which sorting algorithm uses a pivot element?
A) Bubble B) Selection C) Quick D) Insertion
Answer: C
11. Which sorting algorithm uses a pivot element?
A) Bubble B) Selection C) Quick D) Insertion
Answer: C
12. Which sorting algorithm uses a pivot element?
A) Bubble B) Selection C) Quick D) Insertion
Answer: C
13. Which sorting algorithm uses a pivot element?
A) Bubble B) Selection C) Quick D) Insertion
Answer: C
14. Which sorting algorithm uses a pivot element?
A) Bubble B) Selection C) Quick D) Insertion
Answer: C
15. Which sorting algorithm uses a pivot element?
A) Bubble B) Selection C) Quick D) Insertion
Answer: C
16. Which sorting algorithm uses a pivot element?
A) Bubble B) Selection C) Quick D) Insertion
Answer: C
17. Which sorting algorithm uses a pivot element?
A) Bubble B) Selection C) Quick D) Insertion
Answer: C
18. Which sorting algorithm uses a pivot element?
A) Bubble B) Selection C) Quick D) Insertion
Answer: C
19. Which sorting algorithm uses a pivot element?
A) Bubble B) Selection C) Quick D) Insertion
Answer: C
20. Which sorting algorithm uses a pivot element?
A) Bubble B) Selection C) Quick D) Insertion
Answer: C