Data Structures: Array Algorithms - Search, Insert, Delete, and Bubble Sort
Search Algorithm 3: Binary Search
Definition: A sorted array with elements and a specific of information are given. This algorithm finds the location of in or sets .
Algorithm Steps
1. [Initialize]: Set , and .
2. Repeat Steps 3 to 4: While and .
3. Set MID: .
4. If ITEM = DATA[MID]:
Then: Set .
Else if :
Then: Set .
Else:
Set .
5. [Successful?]:
If :
Write: "ITEM is not in the array DATA."
Else:
Write: "LOC is the location of ITEM."
6. Exit.
C++ Implementation
int main() {
int DATA[10] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};
int N = 10;
int ITEM = 23;
int BEG = 0;
int END = N - 1;
int MID;
int LOC = -1;
while (BEG <= END && LOC == -1) {
MID = (BEG + END) / 2;
if (ITEM == DATA[MID])
LOC = MID;
else if (ITEM < DATA[MID])
END = MID - 1;
else
BEG = MID + 1;
}
if (LOC == -1)
cout << "ITEM is not in the array DATA\n";
else
cout << LOC << " is the location of ITEM\n";
return 0;
}
Execution Trace for Binary Search
Objective: Search for
ITEM = 23in arrayDATA = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}.Initial Values:
Iteration 1:
Comparison: , so .
Iteration 2:
Comparison: , so .
Iteration 3:
Comparison: , so .
Result: Exit loop. Display: "5 is the location of ITEM".
Algorithm 4: Insertion into an Array at a Given Location
Definition: An array with elements and a specific and location are given. This algorithm inserts into at position .
Algorithm Steps
1. [Initialize]: If or , then:
Write: "Invalid Location."
Exit.
2. Set K: .
3. Repeat while K ≥ LOC:
Set .
Set .
4. Set DATA[LOC]: .
5. Set N: .
6. Write: Updated array .
7. Exit.
C++ Implementation
#include <iostream.h>
int main() {
int DATA[20] = {10, 20, 30, 40, 50};
int N = 5;
int ITEM = 25;
int LOC = 2;
int K;
if (LOC < 0 || LOC > N) {
cout << "Invalid Location\n";
return 0;
}
for (K = N - 1; K >= LOC; K--) {
DATA[K + 1] = DATA[K];
}
DATA[LOC] = ITEM;
N = N + 1;
cout << "Updated Array:\n";
for (K = 0; K < N; K++)
cout << DATA[K] << '\n';
return 0;
}
Execution Trace for Insertion
Target: Insert
ITEM = 25atLOC = 2.Initial Array:
DATA = {10, 20, 30, 40, 50}, .Shifting Steps:
: (Array becomes
{10, 20, 30, 40, 50, 50}): (Array becomes
{10, 20, 30, 40, 40, 50}): (Array becomes
{10, 20, 30, 30, 40, 50})
Loop Ends: is now less than .
Insertion: (Array becomes
{10, 20, 25, 30, 40, 50}).Update Count: .
Algorithm 5: Deletion from an Array at a Given Location
Definition: An array with elements and a specific location are given. This algorithm deletes the element at position .
Algorithm Steps
1. [Initialize]: If or , then:
Write: "Invalid Location."
Exit.
2. Set ITEM: .
3. Set K: .
4. Repeat while K < N - 1:
Set .
Set .
5. Set N: .
6. Write: Updated array .
7. Exit.
C++ Implementation
int main() {
int DATA[20] = {10, 20, 30, 40, 50};
int N = 5;
int LOC = 2;
int K;
if (LOC < 0 || LOC > N - 1) {
cout << "Invalid Location\n";
return 0;
}
for (K = LOC; K < N - 1; K++) {
DATA[K] = DATA[K + 1];
}
N = N - 1;
cout << "Updated Array:\n";
for (K = 0; K < N; K++)
cout << DATA[K] << "\n";
return 0;
}
Execution Trace for Deletion
Target: Delete element at
LOC = 2.Initial Array:
DATA = {10, 20, 30, 40, 50}, .Shifting Steps:
: (Value
40moves to index 2; Array:{10, 20, 40, 40, 50}): (Value
50moves to index 3; Array:{10, 20, 40, 50, 50})
Loop Ends: reached .
Update Count: .
Final Result: Array is effectively
{10, 20, 40, 50}.
Algorithm 8: Bubble Sort
Definition: An array with elements is given. This algorithm sorts in ascending order using the Bubble Sort method.
Algorithm Steps
1. [Initialize]: Set .
2. Repeat Steps 3 to 5: While .
3. Set K: .
4. Repeat while K ≤ N - PASS - 2:
If , then:
Interchange and .
Set .
5. Set PASS: .
6. Write: Sorted array .
7. Exit.
C++ Implementation
#include <iostream.h>
int main() {
int DATA[10] = {64, 25, 12, 22, 11};
int N = 5;
int PASS, K, TEMP;
for (PASS = 0; PASS < N - 1; PASS++) {
for (K = 0; K < N - PASS - 1; K++) {
if (DATA[K] > DATA[K + 1]) {
TEMP = DATA[K];
DATA[K] = DATA[K + 1];
DATA[K + 1] = TEMP;
}
}
}
cout << "Sorted Array:\n";
for (K = 0; K < N; K++)
cout << DATA[K] << "\n";
return 0;
}
Execution Trace for Bubble Sort
Initial Data:
{64, 25, 12, 22, 11}, .Pass 0:
Compare 64, 25: Swap →
{25, 64, 12, 22, 11}Compare 64, 12: Swap →
{25, 12, 64, 22, 11}Compare 64, 22: Swap →
{25, 12, 22, 64, 11}Compare 64, 11: Swap →
{25, 12, 22, 11, 64}Result of Pass 0: Largest element (64) moved to the end.
Pass 1:
Compare 25, 12: Swap →
{12, 25, 22, 11, 64}Compare 25, 22: Swap →
{12, 22, 25, 11, 64}Compare 25, 11: Swap →
{12, 22, 11, 25, 64}Result of Pass 1: 25 sits in the correct sorted sub-array.
Pass 2:
Compare 12, 22: No swap.
Compare 22, 11: Swap →
{12, 11, 22, 25, 64}
Pass 3:
Compare 12, 11: Swap →
{11, 12, 22, 25, 64}
Final Result: The array is fully sorted in ascending order:
{11, 12, 22, 25, 64}.