Arrays
-
Upload
adeel-ahmed-durrani -
Category
Documents
-
view
214 -
download
0
description
Transcript of Arrays
Data Structures and AlgorithmsArrays
ObjectivesBy the end of this lecture you will be able to:
Define the types of Data Structures Define Array Define Array Operations and Algorithms
Types of Data Structures
Data structures can be classified into two categories:
Linear Non-Linear
Linear Data Structures: are the structures which are arranged in a sequence The elements of such structures can be accessed
linearly
Non-Linear Data Structures: Are the structures which do not have a linear sequence
Cont’d
Type Data Structure
Linear
ArraysStacksQueuesSimple QueueDouble Ended QueueLinked ListsSingly Linked ListDoubly Linked ListHeader Linked ListCircular Linked ListStack and Queues as Linked Lists
Cont’d
Types Data Structure
Nonlinear
TreesSimple TreesBinary TreesOther Trees
Data Types
So far, we have seen only simple data types, such as int, float, and char.
Simple variables can hold only one value at any time during program execution, although that value may change.
A data structure is a data type that can hold multiple values at the same time. (Synonyms: complex data type, composite data type)
The array is one kind of data structure.
Array
Arrays
Linear Array:
An array is a group of related data items that all have the same name and the same data type.
is a list of a finite number n of homogeneous data elements Is one of the Linear Data Structure
Arrays are static in that they remain the same size throughout program execution. An array’s data items are stored contiguously in memory. Each of the data items is known as an element of the array. Each element can be
accessed individually.
The number of elements in an array is called the length or size of the array The lowest index is called the Lower Bound, LB The largest index is called the Upper Bound, UB
Cont’d
The length can be found by the formula:
Length = UB – LB + 1
Array elements can be denoted by subscript notation as:
A1, A2, A3, A4, … An
Or by using the C Language style
A[0], A[1], A[2], … A[n]
Array Declaration and Initialization
int numbers[ 5 ] ; The name of this array is “numbers”. This declaration sets aside a chunk of memory that is big
enough to hold 5 integers. It does not initialize those memory locations to 0 or any
other value. They contain garbage. Initializing an array may be done with an array
initialization, as in : int numbers[ 5 ] = { 5, 2, 6, 9, 3 } ;
5 2 6 9 3myarray =
Accessing Array Elements
Each element in an array has a subscript (index) associated with it.
Subscripts are integers and always begin at zero.
Values of individual elements can be accessed by indexing into the array. For example,
printf(“The third element = %d.\n”, numbers[ 2 ] ) ;
would give the output
The third element = 6.5 2 6 9 3numbers
0 1 2 3 4
Accessing Array Elements (con’t)
A subscript can also be an expression that evaluates to an integer.
numbers[ (a + b) * 2 ] ;
Caution! It is a logical error when a subscript evaluates to a value that is out of range for the particular array. Some systems will handle an out-of-range error gracefully and some will not (including ours). Normally, when you see a file named core (or core*) it means you exceeded the end of an array!
Modifying Elements
Individual elements of an array can also be modified using subscripts.
numbers[ 4 ] = 20 ; /*changes the value of the element
found at subscript 4 to 20 */
Initial values may be stored in an array using indexing, rather than using an array initialization.
numbers[ 0 ] = 5 ;
numbers[ 1 ] = 2 ;
numbers[ 2 ] = 6 ;
numbers[ 3 ] = 9 ;
numbers[ 4 ] = 3 ;
Filling Large Arrays
Since many arrays are quite large, using an array initialization can be impractical.
Large arrays are often filled using a for loop.
for ( i = 0; i < 100; i++ ) { values [ i ] = 0 ; }
would set every element of the 100 element array “values” to 0.
More Declarations
int score [ 39 ] , gradeCount [ 5 ];
Declares two arrays of type int.
Neither array has been initialized.
“score” contains 39 elements (one for each student in a class).
“gradeCount” contains 5 elements (one for each possible grade, A - F).
Using #define for Array Sizes
#define SIZE 39 //pre-processor #define GRADES 5int main ( void ){ int score [ SIZE ] ; int gradeCount [ GRADES ] ;
}
Traversing Arrays
Let LA be a collection of data elements stored in the memory of the computer
Suppose we want to print the elements or count the number of elements
These operations can be done with the help of Traversing
Traversing is the method of accessing and processing (called visiting) each element exactly once
Traversal Algorithms
Algo. (Traversing a Linear Array): Here LA is a linear array with lower bound LB and upper bound UB. This algo traverses LA by applying the PROCESS operation
1.[Initialize counter] Set K := LB2.Repeat step 3 and 4 while K <= UB3. [Visit Element] Apply PROCESS to LA[K]4. [Increase counter] Set K := K + 1
[End of loop]
5.Exit
Traversal Algorithms
Algo. (Traversing a Linear Array): Here LA is a linear array with lower bound LB and upper bound UB. This algo traverses LA with LB and UB
1.Repeat for K = LB to UB
2. Apply PROCESS to LA[K][End of loop]
3.Exit
Array Operations
Insertion DeletionSortingSearchingMergingSplitting
Insertion of an element in an array
Algo: (Insertion into a Linear Array):We will be inserting an ITEM into an Array LA at location P(this algorithm simply replace an item)
1.[Insert] LA [P] := ITEM
2.Exit
Insertion of elements in an array
Algo: (Insertion into a Linear Array): Here LA is a linear array with lower bound LB and upper bound UB .We will be inserting elements into an Array LA
1.[Initialize counter] Set K := LB
2.Repeat step 3-5 while K <= UB
3. Input ITEM
4. [Insert Element] ] LA [K] := ITEM
5. [Increase counter] Set K := K + 1[End of loop]
6.Exit
Searching an element in an array(Linear Search)
Algo: (Searching an element in Linear Array): Here LA is a linear array with lower bound LB and upper bound UB .We will search an elements TARGET in an Array LA. If it is found we will return location ,else we will display message “ITEM NOT FOUND”.
1.INPUT TARGET
2.[Initialize counter] Set K := LB
3.Repeat step 3-7 while K <= UB
4. IF LA[K]==TARGET
5. Return K
6. EXIT
7. [Increase counter] Set K := K + 1[End of loop]
8.PRINT “target item not found in array LA”
9.Exit
Delete
Algo: (Searching an element in Linear Array): Here LA is a linear array with lower bound LB and upper bound UB .We will delete an elements TARGET in an Array LA.
INPUT TARGETCall LINEAR SEARCH(LA,TARGET)IF LOCATION returned K=LINEAR SEARCH(LA,TARGET) Repeat step 3-5 while K < UB Set LA[K] := LA[K + 1] [Increase counter] Set K := K + 1
[End of loop]
1. Set UB:=K2.Exit
Insert Item at specific location
Description: Here A is a sorted linear array with N elements. LOC is the location where ITEM is to be inserted.
1. Set I = N - 1 [Initialize counter]
2. if (Loc >= 0 and Loc < N) then
2a. Repeat While (I >= LOC-1)
3. Set A[I+1] = A[I] [Move elements downward]
4. Set I = I – 1 [Decrease counter by 1] [End of While Loop]
5. Set A[LOC] = ITEM [Insert element]
5a. else
6. Write “Cannot insert value…”
7. [End If]
Sorting
Sorting and Searching are one of the fundamental operations in Computer Science
Sorting is the operation of arranging data in some order
In case of numerical data, the arrangement could be: Increasing Oder or Ascending Order Decreasing Order or Descending Order
In case of character data, the order could be: Alphabetical
Some Definitions
Internal Sort The data to be sorted is all stored in the computer’s
main memory.
External Sort Some of the data to be sorted might be stored in
some external, slower, device.
In Place Sort The amount of extra space required to sort the data
is constant with the input size.
Cont’d
There are many sorting algorithms Selecting a particular algorithm depends on the type of data
and the amount of data Suppose we have a list of numbers, A with n elements, then
the sorting can be defined as:
A[0] < A[1] < A[2] < A[3] < … < A[n] (increasing order)
For example:
10 5 20 18 16 25 27 4 3 1
1 3 4 5 10 16 18 20 25 27 Sorted
Types of Sorting Algorithms
There are many, many different types of sorting algorithms, but the primary ones are:
● Bubble Sort● Selection Sort● Insertion Sort● Merge Sort●Quick Sort● Shell Sort
●Radix Sort● Swap Sort●Heap Sort
Sorting algorithms
bubble sort: swap adjacent pairs that are out of order
selection sort: look for the smallest element, move to front
insertion sort: build an increasingly large sorted front portion
merge sort: recursively divide the array in half and sort it
heap sort: place the values into a sorted tree structure
quick sort: recursively partition array based on a middle value
other specialized sorting algorithms:
bucket sort: cluster elements into smaller groups, sort them
radix sort: sort integers by last digit, then 2nd to last, then ...
Bubble Sort
One of the very basic sorting algorithms It takes (n – 1) steps or passes to sort a list of n numbers Therefore the complexity of this algorithm is O(n2) i.e., it
takes n2 time to sort a list of n numbers Suppose we have a list A[1], A[2], … , A[n] The Bubble sort works as:
1. Compare A[1] and A[2] and arrange them i.e., A[1] < A[2]2. Then compare A[2] and A[3] and arrange them i.e., A[2] < A[3]3. Keep doing this until we have A[n-1] < A[n]
At the end of pass one, the largest element is “bubbled up” to nth position in the list
Each pass requires one less comparison then the last pass
Example
Suppose we have the following list:
Pass 1:
Compare A[0] and A[1] i.e., 32 and 51, As 32 < 51, no element is exchanged
Compare A[1] and A[2], as A[1] > A[2], interchange them
32 51 27 85 66 23 13 57
32 27 51 85 66 23 13 57
Cont’d Compare A[2] and A[3], as A[2] < A[3], items are not exchanged
Compare A[3] and A[4], as A[3] > A[4], interchange them
Compare A[4] and A[5], as A[4] > A[5], exchange
32 27 51 85 66 23 13 57
32 27 51 66 85 23 13 57
Cont’d Compare A[5] and A[6], as A[5] > A[6] therefore we exchange them
Compare A[6] and A[7], as A[6] > A[7], exchange them
32 27 51 66 23 85 13 57
32 27 51 66 23 13 85 57
32 27 51 66 23 13 57 85
Cont’d Pass 2
32 27 51 66 23 85 13 57
27 32 51 66 23 13 85 57
27 32 51 66 23 13 57 85
27 32 51 23 66 13 57 85
27 32 51 66 23 13 57 85
27 32 51 23 13 66 57 85
27 32 51 23 13 57 66 85
Cont’d Pass 3:
27 32 51 23 13 57 66 85
27 32 23 51 13 57 66 85
27 32 23 13 51 57 66 85
Cont’d Pass 4:
27 32 23 13 51 57 66 85
27 23 32 13 51 57 66 85
27 23 13 32 51 57 66 85
Cont’d Pass 5:
27 23 13 32 51 57 66 85
23 27 13 32 51 57 66 85
23 13 27 32 51 57 66 85
Cont’d Pass 6:23 13 27 32 51 57 66 85
13 23 27 32 51 57 66 85
Bubble Sort Algorithm
Algo. BubbleSort(Data, N)
1.Repeat steps 2 and 3 for K = 0 to N -1
2. Set PTR := 0 [initialize pointer]
3. Repeat while PTR <= N – KI. If Data[PTR] > Data[PTR + 1], then
Interchange DATA[PTR] and Data[PTR + 1][End of If]
II. Set PTR := PTR + 1 [End of while]
[End of for]
4.Exit
Bubble Sort: Analysis
Running time: Worst case: O(N2) Best case: O(N)
Variant: bi-directional bubble sort
original bubble sort: only works to one direction
bi-directional bubble sort: works back and forth.
Selection Sort
Idea: Find the smallest element in the array Exchange it with the element in the first position
Find the second smallest element and exchange it with the element in the second position
Continue until the array is sorted
Disadvantage: Running time depends only slightly on the amount of order in the file
Selection Sort: Cont’d
1. Select the “best” (eg. smallest) item from the unsorted group, then put the “best” item at the end of the sorted group.
2. Repeat the process until the unsorted group becomes empty.
Example
1329648
8329641
8349621
8649321
8964321
8694321
9864321
9864321
Selection Sort
Alg.: SELECTION-SORT(A)
n ← length[A]
for j ← 1 to n - 1
do smallest ← j
for i ← j + 1 to n
do if A[i] < A[smallest]
then smallest ← i
exchange A[j] ↔ A[smallest]
1329648
Selection Sort: Analysis
Running time:Worst case: O(N2)Best case: O(N2)
Insertion Sort
Idea: like sorting a hand of playing cards Start with an empty left hand and the cards facing down on the table.
Remove one card at a time from the table, and insert it into the correct position in the left hand
compare it with each of the cards already in the hand, from right to left
The cards held in the left hand are sorted
these cards were originally the top cards of the pile on the table
To insert 12, we need to make room for it by moving first 36 and then 24.
Insertion Sort
6 10 24
12
36
Cont’d
Insertion Sort
6 10 24 36
12
Cont’d
Insertion Sort
6 10 24 36
12Cont’d
Insertion Sort
5 2 4 6 1 3
input array
left sub-array right sub-array
at each iteration, the array is divided in two sub-arrays:
sorted unsorted
Insertion Sort
INSERTION-SORT
Alg.: INSERTION-SORT(A)
for j ← 2 to n
do key ← A[ j ]
Insert A[ j ] into the sorted sequence A[1 . . j -1]
i ← j - 1
while i > 0 and A[i] > key
do A[i + 1] ← A[i]
i ← i – 1
A[i + 1] ← key
Insertion sort – sorts the elements in place
a8a7a6a5a4a3a2a1
1 2 3 4 5 6 7 8
key