[go: up one dir, main page]

0% found this document useful (0 votes)
8 views10 pages

10. 2 ARRAYS and Algorithm (Searching & SOrtin

The document provides a comprehensive overview of arrays, including one-dimensional and two-dimensional arrays, their declaration, and usage in programming. It covers algorithms for sorting (bubble sort) and searching (linear search) within arrays, along with pseudocode and examples in Visual Basic and Python. Additionally, it emphasizes the importance of understanding array bounds and data types for effective programming.

Uploaded by

peacendwar32
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)
8 views10 pages

10. 2 ARRAYS and Algorithm (Searching & SOrtin

The document provides a comprehensive overview of arrays, including one-dimensional and two-dimensional arrays, their declaration, and usage in programming. It covers algorithms for sorting (bubble sort) and searching (linear search) within arrays, along with pseudocode and examples in Visual Basic and Python. Additionally, it emphasizes the importance of understanding array bounds and data types for effective programming.

Uploaded by

peacendwar32
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/ 10

9.2, 10.2, 11.1 to 11.

2 Algorithm & Programming Basics CS 9618 with Majid Tahir


1 at www.majidtahir.com

Syllabus Content:
10.2 Arrays
use the technical terms associated with arrays including upper and lower bound
select a suitable data structure (1D or 2D array) to use for a given task
use pseudocode for 1D and 2D arrays (pseudocode will use square brackets to contain the
array subscript, for example a 1D array as A[1:n] and a 2D array as C[1:m, 1:n])
write program code using 1D and 2D arrays
write algorithms/program code to process array data including:
o Sort using a bubble sort
o Search using a linear search

An array is a special variable that has one name, but can store multiple values. Each value is stored in an
element pointed to by an index.
The first element in the array has index value 0, the second has index 1, etc

One Dimensional Arrays


A one dimensional array can be thought as a list. An array with 10 elements, called names, can store 10
names and could be visualized as this: Lower bound of ARRAY can start from 0 or 1

Index Name Index Name


0 Majid 1 Majid
1 Tahir 2 Tahir
2 Naila 3 Naila
3 Hassan 4 Hassan
4 Adil 5 Adil
5 Ali 6 Ali
6 Osman 7 Osman
7 Abdullah 8 Abdullah
8 Haider 9 Haider
9 Hamza 10 Hamza

Arrays (One-dimensional arrays)


In order to use a one-dimensional array in a computer program, you need to consider:
• What the array is going to be used for, so it can be given a meaningful name
• How many items are going to be stored, so the size of the array can be determined.
• What sort of data is to be stored, so that the array can be the appropriate data type.
DECLARATION of Blank Array with 10 slots:
Pseudocode: VB code example:
DECLARE names[10]: STRING Dim names(9) As String
PYTHON Code
name[]

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 1


9.2, 10.2, 11.1 to 11.2 Algorithm & Programming Basics CS 9618 with Majid Tahir
1 at www.majidtahir.com

Entering Values in One-Dimension Array


BEGIN
DECLARE count : Integer
DECLARE name [5] : String // for declaring 5 elements in ARRAY
DECLARE marks [5] : Integer
FOR count = 1 to 5 // for inputting 5 names and grades
PRINT (“Enter Name “& count)
INPUT name (count)
PRINT (“Enter grade for “& name(count))
INPUT marks (count)
NEXT count
FOR count 1 to 5 // for displaying 5 names and grades
PRINT (name (count) & “has marks " & marks(count))
NEXT count
END

PYTHON Code:
name = []
marks = []
for count in range(5):
name.append (str(input("Enter name: ")))
marks.append (int(input("Enter marks ")))
print("Name ", name, " scored ", marks)

OUTPUT screen

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 2


9.2, 10.2, 11.1 to 11.2 Algorithm & Programming Basics CS 9618 with Majid Tahir
1 at www.majidtahir.com

VB Code in Console Mode


Dim name(5) As String
Dim marks(5) As Integer
For count = 1 To 5
Console.WriteLine("input name " & count)
name(count) = Console.ReadLine()
Console.WriteLine("input marks " & count)
marks(count) = Console.ReadLine()
While marks(count) > 100 Or marks(count) < 0
Console.WriteLine("Re-Enter marks" & count)
marks(count) = Console.ReadLine()
End While
Next
For count = 1 To 5
Console.WriteLine(name(count) & " scored: " & marks(count))
Next
Output of VB code (Console mode)

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 3


9.2, 10.2, 11.1 to 11.2 Algorithm & Programming Basics CS 9618 with Majid Tahir
1 at www.majidtahir.com

Python One-dimensional array with values in it.

num = [1, 2, 3]
num.append(4)
num.extend([5, 6])
print(num) # will print this in output [1, 2, 3, 4, 5, 6]

Another example of One-Dimensional Array


Module Module1
Sub Main()
Dim count As Integer
Dim name(4) As String
Dim marks(4) As Integer
Dim gender(4) As String
For count = 0 To 4
Console.WriteLine("please enter your name" & count)
name(count) = Console.ReadLine()
Console.WriteLine("please enter your gender" & count)
gender(count) = Console.ReadLine()
Console.WriteLine("please enter your marks" & count)
marks(count) = Console.ReadLine()
Next count
For count = 0 To 4
Console.WriteLine("your name is : " & name(count))
Console.WriteLine("your gender is : " & gender(count))
Console.WriteLine("your marks are : " & marks(count))
Next count
Console.ReadKey()
End Sub
End Module

Multi-Dimensional Arrays or Two dimensional Arrays (2D Array):


A multi-dimensional array can be thought of as a table, each element has a row and column index.
Following example declares a two-dimensional array called table with 3 rows and 4 colums
and would be declared in PseudoCode as follows:
DECLARE table(3, 4) : INTEGER

Visual Basic(Console mode)


Dim table(3, 4) : As Integer

Python Code
row, col = 3, 4
table = [[0 for x in range(row)] for y in range(col)]
# Creates a list containing 5 lists, each of 8 items, all set to 0

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 4


9.2, 10.2, 11.1 to 11.2 Algorithm & Programming Basics CS 9618 with Majid Tahir
1 at www.majidtahir.com

PSEUDOCODE Example of Two-Dimension Array


BEGIN
DECLARE table(3, 4) : Integer
FOR row = 1 To 3
FOR column = 1 To 4
PRINT("Please Input Value in Row: ",row, "column : ", column)
INPUT table(row, column)
NEXT
NEXT
FOR row = 1 To 3
FOR column = 1 To 4
PRINT ("Row = " & row & "column = " & column & “has Value”)
PRINT (table(row, column))
NEXT
NEXT
END

VB Code Example of Two-Dimension Array


Sub Main()
Dim table(2, 3) As Integer
For row = 0 To 2
For column = 0 To 3
Console.WriteLine("Please Input Value in Row: " & row & "column : " & column)
table(row, column) = Console.ReadLine()
Next
Next
Console.Clear()

For row = 0 To 2
For column = 0 To 3
Console.WriteLine("Row = " & row & "column = " & column & “has Value”)
Console.WriteLine(matrix(row, column))
Next
Next
Console.ReadKey()
End Sub

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 5


9.2, 10.2, 11.1 to 11.2 Algorithm & Programming Basics CS 9618 with Majid Tahir
1 at www.majidtahir.com

Multi-Dimensional Arrays:
A multi-dimensional array can be thought of as a table, each element has a row and
column index.
Following example declares a two-dimensional array called matrix and would be
declared by

VB Code for 2-D Array is:

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 6


9.2, 10.2, 11.1 to 11.2 Algorithm & Programming Basics CS 9618 with Majid Tahir
1 at www.majidtahir.com

Searching & Sorting Algorithms in an ARRAY


Linear Search:
Linear search is a method of searching a list in which each element of an array is
checked in order, from the lower bound to the upper bound, until the item is found, or
the upper bound is reached.

Searching G in the sorted list via Linear search.

The pseudocode linear search algorithm and identifier table to find if an item is in the
1D array(myList) is given below.
DECLARE count, num As Integer
DECLARE found As Boolean = False
//Creating array to search item (Free notes @ www.majidtahir.com)
DECLARE Mylist() As Integer = {4, 2, 8, 17, 9, 3, 7, 12, 34, 21}
OUTPUT ("Please enter any integer to be checked in List")
INPUT num
For count = 1 To 10
If num = Mylist(count) Then
found = True
End If
Next count

If found = True Then


OUTPUT ("Item Found = ", num)
Else
OUTPUT ("Item Found is unsuccessful")
End If

PYTHON Linear Search Algorithm


MyList = [4,2,8,17,9,3,7,12,34,21]
found = False
num = int(input("Please enter number to be found")) OUTPUT
uperbound = len(MyList)
for index in range(upperbound):
if MyList[index]==num:
found = True
if(found):
print ("item found")
else:
print("item not found")

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 7


9.2, 10.2, 11.1 to 11.2 Algorithm & Programming Basics CS 9618 with Majid Tahir
1 at www.majidtahir.com

Bubble Sort:
Bubble sort is a sorting algorithm that compares each value in the list with the next value, and
swaps the value if not in order. Algorithm is repeated many times in loop to arrange all values in
order. We call it Bubble Sort because small values rise to the top slowly like bubbles rise in
water. After every sorting from start to end of list, biggest value goes at the bottom.

When we have completed the first pass through the entire array, the largest value is in the
correct position at the end of the array. The other values may or may not be in the correct order.
We need to work through the array again and again. After each pass through the array the next
largest value will be in its correct position, as shown in Figure below.

In effect we perform a loop within a loop, a nested loop. This method is known as a bubble
sort. The name comes from the fact that smaller values slowly rise to the top, like bubbles in a
liquid.

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 8


9.2, 10.2, 11.1 to 11.2 Algorithm & Programming Basics CS 9618 with Majid Tahir
1 at www.majidtahir.com

Bubble sort PSEUDOCODE:

DECLARE MyList [10] : INTEGER = {4,2,8,17,9,3,7,12,34,21}


DECLARE count, temp, top : INTEGER
DECLARE swap : BOOLEAN = False
top = LENGTH (MyList())
WHILE top <> 0
FOR count = 1 to (top-1)//(top-1)for loop to run till second last value
IF Mylist(count)> Mylist(count + 1)//compare second last with last value
THEN
temp = Mylist(count)
Mylist(count) = Mylist(count + 1)
Mylist(count + 1) = temp
swap = True
End If
top = top-1
NEXT count
END WHILE

Alternate sample program of (Bubble sort)


DECLARE myList : ARRAYS[0:8] OF INTEGER = [4,2,8,17,9,3,7,12,34,21]
DECLARE upperBound, lowerbound, count, temp, top : INTEGER
DECLARE swap : BOOLEAN

upperBound LENGTH(MyList)
lowerBound 0
top upperBound

REPEAT
FOR index = lowerBound TO (top – 1)
Swap FALSE
IF MyList [count] > MyList [count + l]
THEN Temp MyList[count]
MyList[count] MyList[count + 1]
MyList[count + 1] Temp
swaps TRUE
END IF
NEXT
top top – 1
UNTIL (NOT swap) OR (top = 0)

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 9


9.2, 10.2, 11.1 to 11.2 Algorithm & Programming Basics CS 9618 with Majid Tahir
1 at www.majidtahir.com

Refrences:
Computer Science by David Watson & Helen Williams
Visual Basic Console Cook Book
Computer Science AS and A level by Sylvia Langfield and Dave Duddell
https://www.sitesbay.com/javascript/javascript-looping-statement
http://wiki.jikexueyuan.com/project/lua/if-else-if-statement.html

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 10

You might also like