4.array Techniques
4.array Techniques
TECHNIQUES
CHAPTER 4
INTRODUCTION
•Arrays provide a very special way of storing or organizing
data in a computer’s memory.
•Very simple and efficient way of referring to and performing
computations on collections of data that share some
common attribute.
•An array can be compared to a street with a row of houses.
•The address is made up of two parts: the street name and the
number of the lot.
4.1 Array Order Reversal
•Problem: rearrange the elements in an array so that they appear in
reverse order.
: before reversal
: after reversal
•Step 1:
•Step 2:
•Step 3:
•Step 4:
4.1 Array Order Reversal
•With each step the suffixes on the left are increasing by 1 while
at the same time the suffixes on the right are decreasing by 1.
•Let i denote the increasing suffixes, while [n-i] denote the
decreasing sequences.
•But then when i=1 we find that [n-i] is equal to n-1 rather than n
as we need.
•Thus the decreasing suffix is [n-i+1]
•What should be the range of the variable i?
4.1 Array Order Reversal
•To reverse an array of n elements n/2 exchanges are required.
•After the ith iteration , the first i pairs of elements have been
interchanged. This relation remains invariant.
•The ith pair consists of the ith element and (n-i+1)th element.
•The algorithm will terminate because with each iteration i is
advanced by 1, so eventually (n/2) pairs will have been
exchanged.
•Application: Vector and Matrix processing.
4.1 Array Order Reversal
•Design an algorithm that rearranges the elements of an array
so that all those originally stored at odd suffixes are placed
at even suffixes.
•Homework: Design an algorithm that places the k th element
of an array in position 1, the (k+1) th element in position 2,
etc. The original 1st element is placed at (n-k+1) and so on.
4.2: Array Counting or Histogramming
•Problem: Given a set of n students’ examination marks (in
the range 0 to 100) make a count of the number of students
that obtained each possible mark.
•Solution:
◦ (i) Initialize all elements of the counting array a[0 … 100] to 0.
◦ (ii) for every mark m, add one to the count in location m in the
counting array.
•Applications: Statistical Analysis
4.3: Finding the maximum number in a
set
•Problem: find the maximum number in a set of n numbers.
•Solution
◦ Given an array a[1 … n]
◦ Set max to the first element in the array.
◦ While (array is not completed)
◦ if next element is greater than current maximum max, then assign it to max
◦ Return maximum value “max”.
4.3: Finding the maximum number in a
set
•The number of comparisons needed to find the maximum in
n elements is (n-1)
•Invariant: For all i in the range , when i elements have been
examined the variable max is greater than or equal to all
elements in the range 1 to i
•Homework: Design an algorithm to find the second largest
value in an array of n elements.
4.4 Removal of duplicates from an
ordered array
•Problem: Remove all duplicates from an ordered array and contract the
array accordingly. 0 1 2 3 4 5 6 7 8 9 10 11 12
0 1 2 3 4 5 6 7
•After duplicate removal: a = 2 8 15 23 26 29 30 32
•A duplicate pair is identified when two adjacent elements are equal in value.
•With each comparison only two options are possible:
◦ A pair of duplicates has been encountered
◦ The two elements are different.
4.4 Removal of duplicates from an
ordered array
•The position in the array where each most recently encountered
unique element must be located is determined at each instance by
the number of unique elements met so far.
•Let i be the position where the most recently encountered unique
element was found.
•Let j be the count of the number of unique elements until then.
•Then models the contraction mechanism.
4.4 Removal of duplicates from an
ordered array
•How should the procedure be initialized?
•If there are no duplicates in the array then the repeated assignment is
unnecessary.
•Even if duplicates exist it is only necessary to start shifting the
elements after the first duplicate is encountered.
•Solution: while ()
i=i+1
4.4 Removal of duplicates from an
ordered array
•The while loop will run indefinitely if there are no
duplicates in the array.
•To handle the above point, include another condition to be
tested in the while loop.
◦i < n
•Applications: Data compression and text processing
problems.
4.4 Removal of duplicates from an
ordered array
•To delete duplicates from an array of n elements, (n-1)
comparisons are required.
◦ Best case: 0, worst case: (n-2)
•At the end of each iteration, the variable j represents the
number of unique elements encountered in examining the
first (i-1) pairs of elements.
4.5: Partitioning an Array
•Problem: Given a randomly ordered array of n elements,
partition the elements into two subsets such that all elements ≤
x are in one subset and elements > x are in the other subset.
•a = 0 1 2 3 4 5 6 7 8 9
28 26 25 11 16 12 24 29 6 10
◦ while
•Left movement
◦ while
•Swap