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

Array_searching+sorting

The document discusses array sorting and searching techniques, focusing on Linear Search and Binary Search for searching, as well as Bubble Sort, Selection Sort, and Insertion Sort for sorting. It provides descriptions, time complexities, and example code for each algorithm. The document emphasizes the efficiency and implementation details of these algorithms.

Uploaded by

almasmalik296
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Array_searching+sorting

The document discusses array sorting and searching techniques, focusing on Linear Search and Binary Search for searching, as well as Bubble Sort, Selection Sort, and Insertion Sort for sorting. It provides descriptions, time complexities, and example code for each algorithm. The document emphasizes the efficiency and implementation details of these algorithms.

Uploaded by

almasmalik296
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Array sorting and searching

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:

• Start from the first element.


• Compare each element with the target.
• If a match is found, return the index.
• If the list ends, return “not found”.
#include <iostream>
Example
using namespace std;

int main() {
int arr[100], n, key;

// Input array size


cout << "Enter the number of elements: ";
cin >> n;

// Input array elements


cout << "Enter " << n << " elements:\n";
for (int i = 0; i < n; i++) {
cout << "Element " << i + 1 << ": ";
cin >> arr[i];
}

// Input the element to search


cout << "Enter the element to search: ";
cin >> key;
// Sequential search logic
bool found = false;
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
cout << "Element found at index " << i << " (Position " << i + 1 << ")\n";
found = true;
break;
}
}

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;

while (left <= right) {


return 0;
int mid = (left + right) / 2; }
Common Sorting Techniques in
Arrays
1. Bubble Sort

• 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;

int main() { // Print sorted array


int arr[] = {25, 10, 56, 32, 5};
int size = sizeof(arr) / sizeof(arr[0]); cout << "Sorted array: ";
// Insertion Sort logic for (int i = 0; i < size; i++) {
for (int i = 1; i < size; i++) {
int key = arr[i]; cout << arr[i] << " ";
int j = i - 1;
}
// Move elements that are greater than key
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--; return 0;
}

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

You might also like