Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views15 pages

Cse 203#02

The document provides an overview of arrays, including their definition as a fixed-size, linear data structure that stores similar types of elements. It details operations on arrays such as linear and binary search algorithms, insertion, deletion, sorting (with a focus on bubble sort), and merging of sorted lists. Pseudocode for linear search, binary search, and bubble sort is also included to illustrate these operations.

Uploaded by

md9885397
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 views15 pages

Cse 203#02

The document provides an overview of arrays, including their definition as a fixed-size, linear data structure that stores similar types of elements. It details operations on arrays such as linear and binary search algorithms, insertion, deletion, sorting (with a focus on bubble sort), and merging of sorted lists. Pseudocode for linear search, binary search, and bubble sort is also included to illustrate these operations.

Uploaded by

md9885397
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/ 15

Array

• The simplest data structure (1D array).


• Also called a linear array.
• A fixed sized sequenced structure that stores similar type of
values/elements/items.
• Once declared, the size can not be changed.
• Array index starts with 0.
• Application/uses:??

CSE 203 1
Operations on Array
• Traversing
• Searching:
Linear search: A sequential searching process/algorithm that traverses each
element of a list/data structure until the specific element is found.
 Used for unordered/unsorted list/data structure.
 Searching process stops as soon as the element is found at a specific location.
Binary search: A way to find a specific element from a sorted/ordered list.
 This algorithm repeatedly divides the list into half to search for a specific value and
narrows down to the possible location.
 The main idea is to reduce time complexity.

CSE 203 2
Operations on Array
Linear search algorithm:
• Step 1: Start from the first index of the array, compare the specific/desire
value with the element stored in that index.
• Step 2: If the value matches with the element, stop and return the location at
which the value was found. Successful. Searching process ends here.
• Step 3: If the value does not match with the element, compare the value with
the next element (stored in the next index) in the array.
• Step 4: Repeat Step 3 until a match is found.
• Step 5: If the value is not found (unsuccessful search), then print “Not found”.
• Step 6: End.

CSE 203 3
Operations on Array
Pseudocode for linear search:
Linear search(array, array_size, key_value)
Step 1: Set location = -1.
Step 2: Set index = 0.
Step 3: Repeat step 4 until index < array_size.
Step 4: If array[index] = = key_value Then
set location = index+1
break.
index++.
Step 5: If location = = -1 Then
print “Unsuccessful search."
Else
print “Successful. Found in __ location.”
Step 6: End.

CSE 203 4
Operations on Array
Pseudocode for linear search:

CSE 203 5
Operations on Array
Binary search algorithm:
• Step 1: Divide the list into half and find the middle location.
• Step 2: If the key value matches with the element in the middle location,
then search is successful.
• Step 3: If the value does not match with, then decide which half of the list
would be used for further search (step 4).
• Step 4: If the key value is smaller than the element in the middle location, then
left side sublist of the middle location would be considered. Otherwise
right side sublist would be chosen.
• Step 5: This process continues until the key value is found or to the end of the
list.

CSE 203 6
Operations on Array
How does binary search algorithm work?

CSE 203 7
Operations on Array
Pseudocode for binary search:
Binary Search(array, lower_bound, upper_bound, key_value)
Step 1: Set begin = lower_bound, end = upper_bound, location = - 1.
Step 2: Repeat steps 3 and 4 until begin <=end.
Step 3: Set middle_location = (begin + end)/2
Step 4: If array[middle_location] = = key_value Then
set location = middle_location+1.
break.
Else if array[middle_location] > key_value Then
set end= middle_location - 1
Else
set begin = middle_location + 1
Step 5: If location = = -1 Then
print “Unsuccessful search."
Else
print “Successful. Found in __ location.”
Step 6: End.

CSE 203 8
Operations on Array
• Insertion: Adding an element to an array.
 Do yourself.
• Deletion: Removing an element from an array.
 Do yourself.

CSE 203 9
Operations on Array
• Sorting: Arranging the elements of an array in ascending, descending
or alphabetical order.
Bubble sort algorithm:
 Simple and easy to understand.
 Introduces the basic concepts of sorting.
 Used to sort small size of data.
 Basically sorts in ascending order.
 Called so because at end of each level the largest element is bubbled up/found and placed
in the right place of the list.

CSE 203 10
Operations on Array
How does bubble sort algorithm work?

CSE 203 11
Operations on Array
Pseudocode for bubble sort:
Bubble sort (array, array_size)
Step 1: Repeat steps 2 and 3 for 1 to (array_size-1).
Step 2: Set index=0.
Step 3: Repeat the following steps for 1 to (array_size-1).
If array[index]>array[index+1] Then
Swap the two elements.
index++;
Step 4: Print the sorted list/array.
Step 5: End.

CSE 203 12
Operations on Array
Pseudocode for bubble sort:

CSE 203 13
Operations on Array
• Merging: To combine the elements of two sorted list into a single
sorted list.
• One simple way is to add the second list at the end of the first list and then sort
the new list using a sorting algorithm.
• However, this does not take the advantage that both list are already sorted.

CSE 203 14
Operations on Array
• Pseudocode for merging:

CSE 203 15

You might also like