Array_searching+sorting
Array_searching+sorting
Department CS&IT
🔍 Searching in Arraysv
• Searching is the process of finding the location/index of
a specific element in an array. There are mainly two
types of searching algorithms used in arrays:
• 1. Linear Search (Sequential Search)
• 🔹 Description:
• Check every element in the array one by one.
• Simple but inefficient for large arrays.
• 🔹 Time Complexity:
• Best Case: O(1) (if the item is at the beginning)
• Worst Case: O(n)
What is Sequential Search?
• Sequential Search is a basic search algorithm
that checks each element in a list or array one
by one until it finds the target value or reaches
the end of the list.
• Features:
• Easy to implement.
• Works on unsorted or sorted arrays.
• Time complexity is O(n) in the worst case.
• Not efficient for large datasets.
Algorithm Steps:
int main() {
int arr[100], n, key;
if (!found) {
cout << "Element not found in the array.\n";
}
return 0;
}
2. Binary Search
• Description:
• Only works on sorted arrays.
• Repeatedly divides the search interval in half.
• 🔹 Time Complexity:
• Best Case: O(1)
• Average/Worst Case: O(log n)
Example
#include <iostream>
if (arr[mid] == key) {
using namespace std;
cout << "Element found at index " << mid <<
int main() { " (Position " << mid + 1 << ")\n";
int arr[100], n, key;
found = true;
// Input array size break;
cout << "Enter the number of elements: ";
cin >> n; } else if (arr[mid] < key) {
left = mid + 1;
// Input sorted array elements
cout << "Enter " << n << " elements in ascending order:\n"; } else {
for (int i = 0; i < n; i++) {
right = mid - 1;
cout << "Element " << i + 1 << ": ";
cin >> arr[i]; }
}
}
// Input the element to search
cout << "Enter the element to search: ";
cin >> key;
if (!found) {
cout << "Element not found in the array.\n";
// Binary Search logic
int left = 0, right = n - 1;
}
bool found = false;
• Description:
• Repeatedly swaps adjacent elements if they
are in the wrong order.
• Simple but inefficient for large datasets.
• 🔹 Time Complexity:
• Best: O(n) [when already sorted]
• Average/Worst: O(n²)
Example
#include <iostream> // Swap if elements in wrong order
using namespace std; if (arr[j] > arr[j + 1]) {
int temp = arr[j];
int main() {
arr[j] = arr[j + 1];
int arr[100], n;
arr[j + 1] = temp;
// Input size of array }
cout << "Enter the number of elements: "; }
cin >> n; }
// Input elements
// Output sorted array
cout << "Enter " << n << " elements:\n";
for (int i = 0; i < n; i++) { cout << "Sorted array in ascending order:\n";
cout << "Element " << i + 1 << ": "; for (int i = 0; i < n; i++) {
cin >> arr[i]; cout << arr[i] << " ";
} }
cout << endl;
// Bubble sort logic
for (int i = 0; i < n - 1; i++) {
// Pass i
return 0;
for (int j = 0; j < n - i - 1; j++) { }
2. Selection Sort
• 🔹 Description:
• Repeatedly finds the minimum element and
places it at the beginning.
• 🔹 Time Complexity:
• Always: O(n²)
Example
#include <iostream>
// Swap the found minimum with current
using namespace std;
element
int main() {
int temp = arr[i];
int arr[100], n;
arr[i] = arr[minIndex];
// Input number of elements
cout << "Enter the number of elements: ";
arr[minIndex] = temp;
cin >> n; }
// Input array elements
cout << "Enter " << n << " elements:\n";
// Output sorted array
for (int i = 0; i < n; i++) {
cout << "Element " << i + 1 << ": "; cout << "Sorted array in ascending order:\n";
cin >> arr[i];
}
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
// Selection Sort logic
for (int i = 0; i < n - 1; i++) { }
int minIndex = i; cout << endl;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
return 0;
} }
}
3. Insertion Sort
• 🔹 Description:
• Builds the sorted array one element at a time
by inserting elements into their correct
position.
• 🔹 Time Complexity:
• Best: O(n)
• Worst: O(n²)
Example
#include <iostream>
using namespace std;
arr[j + 1] = key;
}
}
📊 Comparison Table
Time
Sorting
Complexity Space Stable
Algorithm
(Worst)
Bubble Sort O(n²) O(1) Yes
Selection Sort O(n²) O(1) No
Insertion Sort O(n²) O(1) Yes
Thank You