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

Ch 2 - Lists - Part 2 (Updated) (2)

The document outlines methods for managing ordered and unordered lists in data structures, including operations such as insertion, deletion, and searching. It explains the binary search algorithm, emphasizing the necessity for the data to be sorted for effective searching. Examples illustrate how binary search locates a target value within a sorted array.

Uploaded by

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

Ch 2 - Lists - Part 2 (Updated) (2)

The document outlines methods for managing ordered and unordered lists in data structures, including operations such as insertion, deletion, and searching. It explains the binary search algorithm, emphasizing the necessity for the data to be sorted for effective searching. Examples illustrate how binary search locates a target value within a sorted array.

Uploaded by

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

Faculty of Information Technology - Computer Science Department 1

Data Structures

Faculty of Information Technology - Computer Science Department 2


Chapter 2

Lists – Ordered Lists

Faculty of Information Technology - Computer Science Department 3


Outline

 Methods of Ordered List


 Methods Implementation

Faculty of Information Technology - Computer Science Department 4


Methods of Ordered List
 The following methods are for a list S which is represented using an array. Given that
the list S is an Ordered list, and size is the number of elements in the list S.
 isEmpty(): Returns true if S has no elements.
 isFull(): Returns true if the number of S elements equals the array length.

 getFirst(): Returns the first element of the list, S[0].

 getLast(): Returns the last element of the list, S[size-1].

 getElement(i): Returns the element of S in index i; an error condition


occurs if i < 0 or i > size − 1.
 Insert(e): Insert the element e into S at its proper location.
Faculty of Information Technology - Computer Science Department 5
Methods of Unordered List
 delFirst(): Remove from S the element at index 0. An alerting message must be displayed
if
S is empty.
 delLast(): Remove from S the element at index size-1. An alerting message must
be displayed if S is empty.
 delIn(i): Remove from S the element at index i; an error condition occurs
if i < 0 or i > size − 1. An alerting message must be displayed if S is
empty.
 search(e): Search for an element e and return the index of e if exists, otherwise,
return -99999. Since the list is ordered, the Binary Search method will
be used.
 display(): Print the contents of S.
Faculty of Information Technology - Computer Science Department 6
 length(): Returns the number of elements of S.
Binary Search

 For this algorithm to work properly, the data collection should be in the sorted form.
 Binary search looks for a particular item by comparing the middlemost item of
the collection.
 If a match occurs, then the index of the item is returned.
 If the middle item is greater than the item, then the item is searched in the sub-array
to the left of the middle item.
 Otherwise, the item is searched for in the sub-array to the right of the middle item.
 This process continues on the sub-array as well until the size of the subarray reduces
to
zero.

Faculty of Information Technology - Computer Science Department 7


Binary Search Example

 Note that for a binary search to work, it is mandatory for the target array to be
sorted
 The following is our sorted array and let us assume that we need to search
the location of value 31 using binary search.

 First, we shall determine half of the array by using this


formula:
mid = (high + low) / 2;

Faculty of Information Technology - Computer Science Department 8


Binary Search Example

 Here it is, (9 - 0 ) / 2 = 4 (integer value of 4.5). So, 4 is mid of the


array.

 Now we compare the value stored at location 4, with the value being searched, i.e.
31. We find that the value at location 4 is 27, which is not a match.

 As the value is greater than 27 and we have a sorted array, so we also know that the
target value must be in the upper portion of the array.

Faculty of Information Technology - Computer Science Department 9


Binary Search Example

 As the value is greater than 27 and we have a sorted array, so we also know that
the target value must be in the upper portion of the array.

 We change our low to mid + 1 and find the new mid value again.
low = mid + 1;
mid = (high + low) / 2;

Faculty of Information Technology - Computer Science Department 10


Binary Search Example

 Our new mid is 7 now. We compare the value stored at location 7 with our
target value of 31.

 The value stored at location 7 is not a match, rather it is less than what
we are looking for. So, the value must be in the lower part of this
location.

Faculty of Information Technology - Computer Science Department 11


Binary Search Example

 Hence, we change our high and the new mid using. New mid will be
5 high = mid - 1;
mid = (high + low) / 2;

 We compare the value stored at location 5 with our target value. We find
that
it is a match.

 We conclude that the target value 31 is stored at location


5.

Faculty of Information Technology - Computer Science Department 12


Methods Implementation

13
Faculty of Information Technology - Computer Science Department

You might also like