Skip to content

Commit 4faaa08

Browse files
committed
Array All Operations
1 parent ee943c6 commit 4faaa08

File tree

1 file changed

+203
-0
lines changed

1 file changed

+203
-0
lines changed

ArrayAllOperation.cpp

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
// functions prototype
5+
void Display(int arr[], int n)
6+
{
7+
// code of traversal
8+
for (int i = 0; i < n; i++)
9+
{
10+
cout << arr[i] << " ";
11+
}
12+
}
13+
//Insertion of elements of an array
14+
int Insertion(int arr[], int size, int element, int capacity, int index)
15+
{
16+
if (size >= capacity)
17+
{
18+
return -1;
19+
}
20+
for (int i = size - 1; i >= index; i--)
21+
{
22+
arr[i + 1] = arr[i];
23+
}
24+
arr[index] = element;
25+
return 1;
26+
}
27+
28+
//Deletion of elements of an array
29+
void Deletion(int arr[], int size, int index)
30+
{
31+
for (int i = index; i < size - 1; i++)
32+
{
33+
arr[i] = arr[i + 1];
34+
}
35+
}
36+
37+
//Searching
38+
int LinearSearch(int arr[], int size, int element)
39+
{
40+
for (int i = 0; i < size; i++)
41+
{
42+
if (arr[i] == element)
43+
return i;
44+
}
45+
return -1;
46+
}
47+
//sorting
48+
void SelectionSort(int arr[], int size)
49+
{
50+
for (int i = 0; i < size - 1; i++)
51+
{
52+
for (int j = i + 1; j < size; j++)
53+
{
54+
if (arr[j] < arr[i])
55+
{
56+
int temp = arr[j];
57+
arr[j] = arr[i];
58+
arr[i] = temp;
59+
}
60+
}
61+
}
62+
}
63+
//insertion_sorting
64+
void InsertionSort(int arr[], int size)
65+
{
66+
for (int i = 1; i < size; i++)
67+
{
68+
int current = arr[i];
69+
int j = i - 1;
70+
while (arr[j] > current && j >= 0)
71+
{
72+
arr[j + 1] = arr[j];
73+
j--;
74+
}
75+
arr[j + 1] = current;
76+
}
77+
}
78+
//bubble_short
79+
void BubbleSort(int arr[], int size)
80+
{
81+
int counter = 1;
82+
while (counter < size)
83+
{
84+
for (int i = 0; i < size - counter; i++)
85+
{
86+
if (arr[i] > arr[i + 1])
87+
{
88+
int temp = arr[i];
89+
arr[i] = arr[i + 1];
90+
arr[i + 1] = temp;
91+
}
92+
}
93+
counter++;
94+
}
95+
}
96+
//binary_search
97+
int BinarySearch(int arr[], int size, int element)
98+
{
99+
int low, mid, high;
100+
low = 0;
101+
high = size - 1;
102+
cout << "Since Binary search is used in sorting array so first we have to sort array if array is not sorted.\n";
103+
InsertionSort(arr, size);
104+
cout << "Sorted Array : ";
105+
Display(arr, size);
106+
cout << endl;
107+
// Keep searching until low <= high
108+
while (low <= high)
109+
{
110+
mid = (low + high) / 2;
111+
if (arr[mid] == element)
112+
{
113+
return mid;
114+
}
115+
if (arr[mid] < element)
116+
{
117+
low = mid + 1;
118+
}
119+
else
120+
{
121+
high = mid - 1;
122+
}
123+
}
124+
return -1;
125+
}
126+
int main(){
127+
int arr[100] = {12, 54, 16, 47, 25};
128+
int size = 5;
129+
int choice, element, index;
130+
do{
131+
cout << "\nEnter 1 -> For Traversal \n";
132+
cout << "Enter 2 -> For Insertion \n";
133+
cout << "Enter 3 -> For Deletion \n";
134+
cout << "Enter 4 -> For Linear searching \n";
135+
cout << "Enter 5 -> For Selection Sorting \n";
136+
cout << "Enter 6 -> For Bubble Sorting\n";
137+
cout << "Enter 7 -> For Insertion Sorting \n";
138+
cout << "Enter 8 -> For Binary searching \n";
139+
cout << "Enter 9 -> For Display\n";
140+
cout << "Enter 10 -> For Quit\n";
141+
cin >> choice;
142+
switch (choice)
143+
{
144+
case 1:
145+
cout << "Traversal : \n";
146+
Display(arr, size);
147+
break;
148+
case 2:
149+
cout << "Enter the index of the element less than " << size << " : ";
150+
cin >> index;
151+
cout << "Enter the element of array : ";
152+
cin >> element;
153+
Insertion(arr, size, element, 100, index);
154+
size += 1;
155+
break;
156+
case 3:
157+
cout << "Enter the index of the element less than " << size << " : ";
158+
cin >> index;
159+
Deletion(arr, size, index);
160+
size -= 1;
161+
Display(arr,size);
162+
break;
163+
case 4:
164+
cout << "Enter the element of array : ";
165+
cin >> element;
166+
cout << element << " is found at index " << LinearSearch(arr, size, element);
167+
break;
168+
case 5:
169+
cout << "\nBefore sorting of elements of an array\n";
170+
Display(arr, size);
171+
SelectionSort(arr, size);
172+
cout << "\nAfter sorting of elements of an array\n";
173+
Display(arr, size);
174+
break;
175+
case 6:
176+
cout << "\nBefore sorting of elements of an array\n";
177+
Display(arr, size);
178+
BubbleSort(arr, size);
179+
cout << "\nAfter sorting of elements of an array\n";
180+
Display(arr, size);
181+
break;
182+
case 7:
183+
cout << "\nBefore sorting of elements of an array\n";
184+
Display(arr, size);
185+
InsertionSort(arr, size);
186+
cout << "\nAfter sorting of elements of an array\n";
187+
Display(arr, size);
188+
break;
189+
case 8:
190+
cout << "Enter the element of array : ";
191+
cin >> element;
192+
cout << element << " is found at index " << BinarySearch(arr, size, element);
193+
break;
194+
case 9:
195+
Display(arr,size);
196+
break;
197+
default:
198+
break;
199+
}
200+
}
201+
while(choice!=10);
202+
return 0;
203+
}

0 commit comments

Comments
 (0)