Thursday, February 2, 2017

Lecture 13 - Data Structures and Algorithms


Array Searching
Here we are looking at algorithms which are used to lookup for a particular value in an array. There are two main techniques considered; binary search and rinear search.

1. Linear Search
Is a very simple technique where the array is looked up for the search item stating from the first element one after the other until the value is found or array end is reached. Can be used on both ordered and un-ordered arrays

Pascal function which performs a Linear search on an integer array. Assuming that array is defined globally.

FUNCTION LinearSearch (ITEM: Integer): Boolean;
VAR
lndex : integer;
Found . Boolean;
BEGIN
Index := 1;
Found := False;

WHILE Found=False AND Index <= TableSize DO
BEGIN

IF ITEM = Table I [index ] THEN
Found :=True
ELSE

Index := Index +1;
END;

LinearSearch := Found;
END;


2. Binary Search
This is a more efficient technique that can be used on a sorted .array. Here the search item is compared with the middle element of the array and if found search ends. If not found the process is continued selecting the upper or lower half of the array. With this chosen half same procedure is continued by selecting it's middle element.

Pascal function which performs a Binary search on an Integer array. Assuming that array is defined globally and sorted in ascending order.

FUNCTION BinarySearch( ITEM : lnteger) : Boolean;
VAR
Low, High, Mid : Integer;
Found : Boolean;

BEGIN
Low := 1;
High := TableSize;
Found := False:

WHILE (Found=Fatse) AND (Low <= High) DO

BEGIN

Mid := ( Low + High )DIV 2;
 

IF ITEM = Table [Mid] THEN
Found := True
ELSE IF ITEM > Table [Mid] THEN
Low := Mid +1
ELSE
High := Mid - 1;
END;


BinarySearch := Found;

END;


Array Sorting

There are number of well defined techniques used for arranging the contents of any array in ascending or descending order..some simple techniques are described in the following section.

1. Selection Sort
If the array has N number of elements there will be N-1 Passes. In each pass the smallest element from the unsorted part of the array is selected and moved at the end of the pass to the head element of the unsorted part. The following diagram illustrates the first pass of the process.

Pascal procedure which performs a Selection sort on an Integer array. Assuming that array is defined globally.


PROCEDURE SelectionSort;
VAR

Index, Small, Current : integer;
Begin

Index := 1;
WHILE Index < TableSize DO
BEGIN
Small := Index;
Current := Index + 1;

WHILE Current <= TableSize DO
BEGIN

IF Table [Current ] < Table [Small ] THEN
Small := Current;
Current := Current + '1;
END;

Swap ( Table[Index], Table[Small] );

Index := Index + 1;
END;
END;



2. Bubble Sort

This sorting technique compares the adjacent elements of the array and interchange them if they are out of order. This process is done until a pass is completed without making any interchanges. Then the array is sorted.

Pascal procedure which performs a Bubble sort on an integer array. Assuming that array is defined globally.

PROCEDURE BubbleSort;
VAR
Right, Left, Target : integer;

BEGIN
Target := TableSize;

WHlLE Target > 1 DO
BEGIN
Left :=1;
Right := 2;

WHILE Right <= Target DO
BEGIN

IF Table [Left] > Table[Right] THEN
SWAP (Table[Left], Table[Right]);
Left := Left + 1;

Right := Right + 1;
END;

Target .= Target -1;
END;
END;



3. Insertion Sort
Insertion sort is a different technique where each item is inserted to the proper place by shifting down the already sorted elements to get space for the new one. This technique is quite useful when adding new items one by one to an already sorted list. The process starts with inserting second element and then third, fourth and so on up to the last.

Pascal procedure which performs an insertion sort on an integer array. Assuming that array is defined globally.

PROCEDURE InsertionSort;
VAR
Next, Prev, Insert_Item : integer;

BEGIN
Next := 2;

WHILE Next <= TableSize DO
BEGIN
Insert_Item := Table [Next];
Prev := Next - 1;

WHILE Prev > 0 AND Insert_Item < Table [Prev] DO
BEGIN
Table[ Prev + 1 ] := Table[ Prev]; { Shifting down elements }
Prev := Prev - 1;
END;

Table [Prev + 1] := Insert_Item;
END;
END;



Linked List

This is a dynamic data structure that can be created by using pointers. Unlike arrays a linked list can grow and compressed according to the requirements as the memory is allocated at run-time using pointers.

First we take a look at pointers and how they are used before we construction of linked list.

What is Pointer ?
It is a variable that can point to another memory location. These pointers can be used create dynamic variables, which are allocated memory at run-time.


Constructing a Linked List with Pointers to Records
To construct linked list we need to have a special record type where one filed is a pointer to another similar record. This allows us to link one node to another forming a chain of nodes. This can be very useful creating data structures such as stack, queue, list, trees and graphs.


1 comment:


  1. I enjoy what you guys tend to be up too. This sort of clever work and exposure! Keep up the great works guys I've incorporated you guys to my personal blogroll. gmail log in

    ReplyDelete

Important Notice!

Dear students and friends. When you commenting please do not mention your email address. Because your email address will be publicly available and visible to all. Soon, it will start sending tons of spams because email crawlers can extract your email from feed text.

To contact me directly regarding any inquiry you may send an email to info@bcslectures.website and I will reply accordingly.