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

Arrays

The document provides an overview of arrays, including their definition, types (1D and 2D), and importance in storing and managing data. It also explains linear search and bubble sort algorithms, detailing their processes, pseudocode, and benefits. The document emphasizes the simplicity and flexibility of linear search for small datasets and the sorting efficiency of bubble sort for small lists.

Uploaded by

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

Arrays

The document provides an overview of arrays, including their definition, types (1D and 2D), and importance in storing and managing data. It also explains linear search and bubble sort algorithms, detailing their processes, pseudocode, and benefits. The document emphasizes the simplicity and flexibility of linear search for small datasets and the sorting efficiency of bubble sort for small lists.

Uploaded by

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

Arrays

Definition

• Array: A data structure that stores multiple elements of the same data
type, which can be accessed using the same identifier.

• Index: The position of an element in an array, starting from the lower


bound (first element) to the upper bound (last element).

Types of Arrays

1. One-Dimensional (1D) Arrays:

o A linear structure referred to as a list.

o Example:

▪ Lower Bound: 0

▪ Upper Bound: 8

Declaration in Pseudocode:

DECLARE myList : ARRAY[0:8] OF INTEGER

Accessing elements: Use the index, e.g myList[7] ← 16.

Two-Dimensional (2D) Arrays


o Represented as a table with rows and columns.

o Example:

Lower Bound: Rows (LBR) and Columns (LBC) start from 0.

▪ Upper Bound: Rows (UBR) = 8, Columns (UBC) = 2.

Declaration in Pseudocode:

DECLARE myArray : ARRAY[0:8, 0:2] OF INTEGER

o Accessing elements: Use row and column indices,

e.g myArray[7,0] ← 16.

Importance of Arrays

• Efficiently store and manage multiple data items of the same type.

• Arrays enable:

1. Storing ordered data (e.g., names, temperatures).


2. Easy searching, sorting, and accessing data.

Linear Search

What is a Linear Search?

✓ A linear search is a method of searching for a specific value (item) in an


array by checking each element one by one, starting from the lower
bound to the upper bound.

Process:

✓ The search starts at the first element.


✓ Each element is compared with the target item.
✓ If the item is found, the search ends.
✓ If the end of the array is reached without finding the item, it means the
item is not present.

Example of Linear Search in Pseudocode

To search for an item in a 1D array (myList), the algorithm follows these steps:

Declare Variables:

i. myList: The array to be searched.


ii. upperBound: Index of the last element in the array.
iii. lowerBound: Index of the first element in the array.
iv. index: Current position in the array during the search.
v. item: The value to search for.
vi. found: A boolean flag to indicate whether the item has been
found.

Steps:

DECLARE myList : ARRAY[0:8] OF INTEGER


DECLARE upperBound, lowerBound, index, item : INTEGER

DECLARE found : BOOLEAN

upperBound ← 8

lowerBound ← 0

OUTPUT "Please enter the item to be found"

INPUT item

found ← FALSE

index ← lowerBound

REPEAT

IF item = myList[index] THEN

found ← TRUE

ENDIF

index ← index + 1

UNTIL (found = TRUE) OR (index > upperBound)

IF found THEN

OUTPUT "Item found"

ELSE

OUTPUT "Item not found"

ENDIF
Explanation

✓ REPEAT...UNTIL Loop: Repeats the search process until:

▪ The item is found (found = TRUE), or

▪ The search reaches beyond the upper bound (index >


upperBound).

✓ The loop stops immediately when the item is found, saving unnecessary
comparisons.

Identifier Table (Explanation of Variables)

Identifier Description

item The integer to be searched.

myList The array being searched.

upperBound The index of the last element in the array.

lowerBound The index of the first element in the array.

index The current element being checked during the search.

found A flag to indicate whether the item has been found.

Benefits of Linear Search

i. Simplicity: Easy to implement.


ii. Flexibility: Can be used with unsorted arrays.
iii. Use Cases:

o Searching small datasets.


o Applications where the data structure cannot be sorted
beforehand.

Bubble Sort Algorithm

Bubble sort is a method to sort items in a meaningful order (e.g alphabetical or


numerical). It repeatedly compares adjacent items in a list and swaps them if
they are in the wrong order. This process continues until no more swaps are
needed, and the list is sorted.

✓ Goal is to sort data in ascending or descending order.

Process:

o Compare adjacent elements.

o Swap them if they are in the wrong order.

o Repeat the process for all elements until no swaps are needed.

✓ Works well for small datasets.


✓ Becomes inefficient for large datasets due to multiple comparisons and
swaps.
Pseudocode :

DECLARE myList : ARRAY[0:8] OF INTEGER

DECLARE upperBound, lowerBound, index, temp : INTEGER

DECLARE swap : BOOLEAN

upperBound <= 8

lowerBound <= 0

top = upperBound

REPEAT

swap <= FALSE

FOR index <= lowerBound TO top - 1

IF myList[index] > myList[index + 1] THEN

temp <= myList[index]

myList[index] <= myList[index + 1]

myList[index + 1] <= temp

swap <= TRUE

ENDIF

NEXT index

top = top - 1

UNTIL NOT swap OR top = 0


Variables Use are:

Variable Description

myList Array to be sorted.

upperBound Last index of the array.

lowerBound First index of the array.

index Tracks current position in the array.

top Last index to compare during a pass.

temp Temporary storage for swapping.

swap Boolean to check if a swap occurred.

Steps of Bubble Sort:

✓ First Pass:

o Compare all adjacent elements.

o Swap elements if needed.

o The largest value moves to the end of the list.

✓ Subsequent Passes:

o Reduce the range of comparison (exclude the sorted elements).

o Continue swapping until no swaps are required.

✓ Final Pass:

o No swaps are made, indicating the list is sorted.


Example of Sorting:

i. Initial list: [19, 27, 36, 16, 42, 21, 89, 55]
ii. Pass 1: [19, 27, 16, 36, 21, 42, 55, 89]
iii. Pass 2: [19, 16, 27, 21, 36, 42, 55, 89]
iv. Pass 3: [16, 19, 21, 27, 36, 42, 55, 89]
v. Sorted list: [16, 19, 21, 27, 36, 42, 55, 89]

You might also like