-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrtsrh.cpp
61 lines (50 loc) · 1.5 KB
/
srtsrh.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<iostream>
using namespace std;
// Function to perform bubble sort
void bubbleSort(int arr[], int size) {
for (int i = 0; i < size - 1; ++i) {
for (int j = 0; j < size - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
// Swap elements if they are in the wrong order
swap(arr[j], arr[j + 1]);
}
}
}
}
// Function to perform linear search
int linearSearch(const int arr[], int size, int key) {
for (int i = 0; i < size; ++i) {
if (arr[i] == key) {
return i; // Return the index if the key is found
}
}
return -1; // Return -1 if the key is not found
}
// Function to print array elements
void printArray(const int arr[], int size) {
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
const int size = 5;
int arr[size] = {64, 25, 12, 22, 11};
cout << "Original array: ";
printArray(arr, size);
// Sorting the array using bubble sort
bubbleSort(arr, size);
cout << "Sorted array: ";
printArray(arr, size);
// Searching for an element using linear search
int key;
cout << "\nEnter the element to search: ";
cin >> key;
int result = linearSearch(arr, size, key);
if (result != -1) {
cout << "Element found at index: " << result << endl;
} else {
cout << "Element not found in the array." << endl;
}
return 0;
}