Array Chapter Notes
Array Chapter Notes
1 50 60 70 80
2 15 45 23 90
Physical View :
00 01 02 03 10 11 12 13 20 21 22 23
10 20 30 40 50 60 70 80 15 45 23 90
500 502 504 506 508 510 512 514 516 518 520 522
• Address Calculation : int a[3][4];
Address of a[i][j] = Base address + (i * c + j) *
sizeof(element size)
Total No. of columns (c) = 4
Address of a[2][3] = 500 + (2 * 4 + 3 ) * 2
= 500 + (11) * 2
= 500 + 22
= 522
Address of a[1][1] = 500 + (1 * 4 + 1 ) * 2
= 500 + (5) * 2
= 500 + 10
= 510
• Address Calculation : int a[3][4]
1. Column wise calculation: Logical View
0 1 2 3
0 10 20 30 40
1 50 60 70 80
2 15 45 23 90
Physical View :
00 10 20 01 11 21 02 12 22 03 13 23
10 50 15 20 60 45 30 70 23 40 80 90
500 502 504 506 508 510 512 514 516 518 520 522
• Address Calculation : int a[3][4];
Address of a[i][j] = Base address + (j * r + i) *
sizeof(element size)
Total No. of rows (r) = 3
Address of a[2][3] = 500 + (3 * 3 + 2 ) * 2
= 500 + (11) * 2
= 500 + 22
= 522
Address of a[1][1] = 500 + (1 * 3 + 1 ) * 2
= 500 + (4) * 2
= 500 + 8
= 508
Array Applications :
1. Representation Of Sparse Matrix.
2. Searching
3. Sorting
4. Polynomial Representation
5. Static Implementation of Stack.
6. Static Implementation of Queue.
7. Static Implementation of Binary Tree.
8. Representation Graph.
Searching :
It is the process of retrieving or locating information
matching some specific criteria or value.
Retrieval :
A successful search is called retrieval.
Methods of Searching :
1. Linear Search.
2. Binary Search.
Array as a Data Structure
Linear Search :
1. It is also called as sequential search.
2. It is simplest form of searching.
3. It can be applied to sequential storage structures like file, array or
linked lists.
Steps :
1. Searching begins from first element or record.
2. the required value is compared with record key.
3. Searching continues sequentially till the record with matching key
value is found or if the data ends.
#include<stdio.h>
int linsearch(int a[], int n, int key)
{
int i,k;
for(i=0;i<n;i++)
{
if(a[i]==key)
return i;
else
k= -1;
}
return k;
}
void main()
{
int a[10],n, I, key, pos;
printf(“How many values”);
scanf(“%d”,&n);
printf(“Enter actual values”);
for(i=0;i<n;i++)
{
Scanf(“%d”, &a[i]);
}
printf(“Enter the value to be searched”);
scanf(“%d”,&key);
pos = linsearch(a,n,key);
if(pos==-1)
printf(“\n Element not found”);
else
printf(“\n Element found at position %d”, pos);
}
Efficiency :
Best case : Ο(1)
worst case : Ο(n)
Advantages :
1. It is very simple method.
2. It does not require data to be ordered.
3. It does not require any additional Data Structures.
Disadvantages : 1. It is very inefficient as its time complexity is Ο(n) .
Improving η Of Linear Search :
The following techniques are used to improve efficiency:
1. Organize data in sorted manner.
2. Arrange the records by frequency of access.
3. Move-to-Front.
• Advantages:
1. It is a very simple method.
2. No additional Data Structure is required.
3. Improves η of sequential search.
• Disadvantages:
• 1. Data has to be sorted.
• 2. The worst case time complexity is still Ο(n) .
int improvedlinsearch(int a[], int n, int key)
{
int i,k;
for(i=0;i<n;i++)
{
if(a[i]==key)
return i;
if(a[i]>key)
break;
else
k= -1;
}
return k;
}
Binary Search :
1.The set of elements is partitioned into two equal parts.
2.The required key is compared with key of middle record.
3.If a match is found, the search terminates.
4.If key is less than middle key, the search proceeds in the top half.
5.If key is greater than middle key, the search proceeds in the upper
half of the table.
6.The process continues till no more partitions are possible.
Binary Search : This searching method allows us to implement by
two ways :
1. Recursive Method.
2. Nonrecursive Method.
Time Complexity : Ο(log2n)
Advantages :
1. It is very efficient as time complexity is Ο(log2n)
2. Does not require additional Data Structure.
Disadvantages :
1. Data has to be in sorted manner.
2. This method can only be applied to sequential or linear Data
Structures.
Non Recursive :
int binsearch(int a[], int n, int key)
{
int mid, top, bottom;
top =0;
bottom = n-1;
while(top<bottom)
{
mid = (top+bottom)/2;
if(a[mid]==key)
return mid;
else
if(key <a[mid])
bottom =mid – 1;
else
top = mid +1;
}
return -1;
}
Recursive :
int recbinsearch(int a[ ], int top, int bottom, int key)
{
int mid;
if(top<=bottom)
{
mid = (top +bottom)/2;
If(a[mid]==key)
return mid;
else if(key < a[mid])
return recbinsearch(a, top, mid-1, key);
else
return recbinsearch(a, mid+1, bottom, key);
}
return -1;
}
Program for non recursive function
void main()
{
int a[10], n, i, key,pos;
printf(“How many values”);
scanf(“%d”,&n);
printf(“Enter actual values”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
bubble(a,n);
printf(“Enter the value to be searched”);
scanf(“%d”,&key);
pos = binsearch(a, n, key);
if(pos==-1)
printf(“\n Element not found”);
else
printf(“\n Element found at position %d”, pos);
}
Program for recursive function
void main()
{
int a[10], n, i, key,pos;
printf(“How many values”);
scanf(“%d”,&n);
printf(“Enter actual values”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
bubble(a,n);
printf(“Enter the value to be searched”);
scanf(“%d”,&key);
pos = recbinsearch(a, 0,n-1, key);
if(pos==-1)
printf(“\n Element not found”);
else
printf(“\n Element found at position %d”, pos);
}
Sorting:
1.File : A file is a collection of records r[0]-------r[n-1].
2.Record : A record is a collection of fields.
3.Record Key : One or more fields of a record may be designated as key
field i.e k[i] is associated with record r[i].
4.Sorted File : A file is said to be sorted on the key if i<j implies that k[i]
precedes k[i].
5.Internal Sorting : Sorting is done on data which is stored in main
memory.
6.External Sorting : Sorting is done on data stored in auxilary storage
devices.
7.In-Place sorting : A sorting algorithm is said to be in-place if it does
not use a lot of additional memory for sorting.
8.Stable Sorting : If relative order of record keys are same in both
original and sorted file, then file is said to be stable.
comparison sort :
A comparison sort is a type of sorting algorithm that only reads the list
elements through a single abstract comparison operation (often a "less
than or equal to" operator or a three-way comparison) that determines
which of two elements should occur first in the final sorted list. The only
requirement is that the operator forms a total preorder over the data,
with:
if a ≤ b and b ≤ c then a ≤ c .
for all a and b, a ≤ b or b ≤ a .
It is possible that both a ≤ b and b ≤ a; in this case either may come first
in the sorted list.
Lower bound on comparison based sorting :
Comparison sorts can be viewed abstractly in terms of decision trees. A
decision tree is a full binary tree that represents
the comparisons between elements that are performed by a
particular sorting algorithm operating on an input of a given size.
Bubble Sort :
1. Pass the file sequentially several times.
2. In each pass, we compare successive pairs of elements (x[i] with
x[i+1]).
3. Interchange the two if they are not in required order.
4. One element is placed in its correct position in each pass.
5. A total n-1 passes are required to sort ‘n’ keys.
Advantages :
1. In-place.
2. Simple.
3. Stable.
Disadvantages :
1. Best & worst efficiency Ο(n2) .
2. Even if elements are in sorted order, then also all n-1 passes will be
required.
• Algorithm :
1. Start.
2.pass = 1
3. i=0
4. if(x[i]>x[i+1])
interchange x[i] and x[i+1]
5. i= i + 1
6. if(i<n-pass)
goto step 4
7. Pass = pass + 1
8. If (pass < n)
goto step 3
9. stop
Improved η of Bubble sort :
1. It is used to avoid unnecessary passes.
2. We have to check whether the array has got sorted at the end of
each pass
3. This can be done by finding out if there were any interchange made
in a pass.
4. If no interchange were done, it implies that the array has got sorted.
5. It can be checked by using a flag.
Example :
17, 29, 58, 42, 33
Pass I 17 29 58 42 33
t=0
17 29 58 42 33
t=0
17 29 58 42 33
t=1
17 29 42 58 33
t=1
17 29 42 33 58
as t=1, go to pass II
Example :
17, 29, 58, 42, 33
t=0
Pass II 17 29 42 33 58
t=0
17 29 42 33 58
t=0
17 29 42 33 58
t=1
17 29 33 42 58
t=1
As t=1, go to pass III
There is no need to go to forward all remaining passes.
Example :
17, 29, 58, 42, 33
t=0
Pass III 17 29 33 42 58
t=0
17 29 33 42 58
t=0
17 29 33 42 58
t=0
17 29 33 42 58
t=0
Compare t==0, break
• Algorithm :
1. Start.
2.pass = 1
3. i=0
4. if(x[i]>x[i+1])
{
interchange x[i] and x[i+1]
t=1;
}
5. i= i + 1
6. if(i<n-pass)
goto step 4
7.if(t==0)
break;
8. Pass = pass + 1
9. If (pass < n)
goto step 3
10. stop
void bubble(int x[], int n)
{
int pass, i, temp, t=0;
for(pass=1;pass<n; pass++)
{
for(i=0; i<n-pass; i++)
{
cc++;
if(x[i]>x[i+1]
{
sc++;
temp=x[i];
x[i]=x[i+1];
x[i+1]=temp;
t=1;
}
}
If(t==0)
Break;
}
• Insertion Sort:
• We select one element from the unsorted set at a time & insert it
into its correct position in the sorted set.
• The method places an unsorted element into its correct position in a
growing sorted list of data.
• Efficiency :
Best case: O(n).
Worst case : O(n2).
• Advantges:
1. Simple.
2. No additional Data structures is required.
3. In-place sort.
4. Stable sort.
5. Best case efficiency O(n).
6. Most suitable sorting method if the elements are almost sorted.
• Disadvantages:
1. Worst case efficiency is O(n2).
• Example:
27, 9, 7, 39, 35, 2
Sorted List Unsorted List
Empty 27, 9, 7, 39, 35, 2
27, 9,7, 39, 35, 2
9, 27 7, 39, 35, 2
7, 9, 27 39, 35,2
7, 9, 27, 39 35, 2
7, 9, 27, 35 , 39 2
2, 7, 9, 27, 35, 39 Empty
• Example:
48, 17, 25, 89, 55, 8
Sorted List Unsorted List
Empty 48, 17, 25, 89, 55, 8
48 17, 25, 89, 55, 8
17, 48 25, 89, 55,8
17,25, 48 89, 55, 8
17, 25, 48, 89 55, 8
17, 25, 48, 55 ,89 8
8, 17, 25, 48, 55, 89 Empty
59, 35, 47, 78, 65, 18
Sorted List Unsorted List
Empty 59, 35, 47, 78, 65, 18
59 35, 47, 78, 65, 18
35, 59 47, 78, 65, 18
35, 47, 59 78, 65, 18
35, 47, 59, 78 65, 18
35, 47, 59, 65, 78 18
18, 35, 47, 59, 65, 78 Empty
Niraj, anita, ajit, sujit, mitali
Sorted List Unsorted List
Empty niraj, anita, ajit, sujit, mitali
Niraj anita, ajit, sujit, mitali
Anita, niraj ajit, sujit, mitali
Ajit, anita, niraj sujit, mitali
Ajit, anita, niraj, sujit mitali
Ajit, anita, mitali, niraj,sujit Empty
37, 15, 7, 92, 57, 68, 2
Sorted List Unsorted List
Empty 37, 15, 7, 92, 57, 68, 2
37 15, 7, 92, 57, 68, 2
15, 37 7, 92, 57, 68, 2
7 , 15 , 37 92, 57, 68, 2
7, 15, 37, 92 57, 68,2
7, 15,37, 57, 92 68,2
7, 15,37, 57 , 68, 92, 2
2, 7, 15, 37, 57, 68, 92 Empty
• Algorithm :
1. Start
2. k=1
3. Key = x[k]
4. i= k-1
5. while (( x[i]>key) && (i>=0),i--
X[i+1] = x[i];
6. x[i+1]= key.
7. k++
8. if(k<n)
Go to 3
9. stop
• Function :
void insertion(int x[], int n)
{
int i,k,key;
for(k=1; k<n;k++)
{
Key=x[k];
for(i=k-1; i>=0;i--)
{
If(x[i]>key)
x[i+1]=x[i];
else
break;
}
X[i+1]=key;
}
}
Divide And Conquer Strategy :
A divide and conquer algorithm is a strategy of solving a large
problem by
1. breaking the problem into smaller sub-problems
2. solving the sub-problems, and
3. combining them to get the desired output.
• To use divide and conquer algorithms, recursion is used.
Steps :
• Divide : Divide the given problem into sub-problems using recursion.
• Conquer: Solve the smaller sub-problems recursively. If the
subproblem is small enough, then solve it directly.
• Combine: Combine the solutions of the sub-problems which is part
of the recursive process to get the solution to the actual problem.
1. Merge Sort.
2. Quick Sort.
Merge Sort : Merging is a process of combining two or more sorted data
lists into a third lists such that it is also sorted.
Steps :
1. The elements to be sorted are divided into two equal parts.
2. Each part is successively divided into two parts till we reach a point
where sub-part is sorted.
3. This will happen when size = 1.
4. These two adjacent sub-lists can be merged to create a sorted sub-
lists.
5. The merging process continues till only one lists of size n is
obtained.
34, 12, 67, 56, 46, 23, 78
0 1 2 3 4 5 6
Mid= low+high/2 (0+6)/2= 6/2=3
[34, 12, 67, 56] [46, 23, 78]
0 1 2 3 4 5 6
[34, 12] [67, 56] [46, 23] [78]
[34] [12] [67] [56] [46] [23] [78]
[12 34] [56 67] [23 46] [78]
[12, 34, 56, 67] [23, 46, 78]
[12, 23, 34, 46, 56, 67, 78]
12, 23, 34, 46, 56, 67, 78
14, 2, 99, 34, 23, 67, 100, 107
[14, 2, 99, 34] [23, 67, 100, 107]
[14, 2] [99, 34] [23, 67] [100, 107]
[14] [2] [99] [34] [23] [67] [100] [107]
[2, 14] [34, 99] [23, 67] [100, 107]
[2, 14, 34, 99] [ 23, 67, 100, 107]
[2, 14, 23, 34, 67, 99, 100, 107]
2, 14, 23, 34, 67, 99, 100, 107
[20] [40] [50] [15] [30] [35] [10] [5]
Merge
[20, 40] [15, 50] [30,35] [5, 10]
[15, 20, 40,50] [5, 10, 30, 35]
[5, 10, 15, 20, 30, 35, 40,50]
5, 10, 15, 20, 30, 35, 40, 50
#include<stdio.h>
void mergesort(int a[], int low, int high)
{
int mid;
if(low<high)
{
mid = (low+high)/2;
mergesort(a, low,mid);
mergesort(a, mid+1,high);
merge(a,low,mid,high);
}
}
void merge(int a[], int low, int mid,int high)
{
int I, j, k, b[10];
i=low;
j=mid+1;
k=0;
while((i<=mid)&&(j<=high))
{
if(a[i]<a[j])
{
b[k]=a[i];
i++;
k++;
}
else
{
b[k]=a[j];
j++;
k++;
}
}
while(i<=mid)
{
b[k]=a[i];
i++;
k++;
}
while(j<=high)
{
while(j<=high)
{
b[k]=a[j];
k++;
j++;
}
for(i=low,k=0;i<=high;i++,k++)
a[i]=b[k];
}
Advantages :
1. Best & worst case efficiency is Ο(nlogn).
2. It is stable sorting process.
Disadvantages : 1. It is not in-place sort.
Quick Sort : It is also called as “Partition Exchange sort”.
Steps :
1. Consider one element at a time as pivot and place it in its correct
position.
2. The pivot is placed such that all elements to the left of pivot are
smaller than the pivot.
3. All elements to the right are greater than pivot.
4. This partitions the array into two parts left partition & right partition.
5. The method is applied for each partition.
6. The process continues till no more partitions can be made.
27, 56, 12, 89, 34,5,100
[12,5] 27 [56, 89, 34,100]
[5 ] 12 27 [34 ] 56 [89, 100]
5 12 27 34 56 89 [100]
5, 12, 27, 34, 56, 89, 100
89, 14, 10, 25, 99, 75, 12, 77, 97
[14, 10, 25, 75, 12, 77] 89 [ 99,97]
[10, 12] 14 [25, 75, 77] 89 [97] 99
10 [12] 14 25 [75, 77] 89 97 99
10 12 14 25 75 [77] 89 97 99
10 12 14 25 75 77 89 97 99
10, 12, 14, 25, 75, 77, 89,97,99
52, 14, 47, 7, 17, 11, 29,9 ,27, 65
[14, 47, 7, 17, 11,29, 9, 27] 52 [65]
[7, 11, 9] 14 [47, 17, 29, 27] 52 65
7 [11, 9] 14 [17, 29, 27 ] 47 52 65
7 [9] 11 14 17 [29, 27] 47 52 65
7 9 11 14 17 [27] 29 47 52 65
7 9 11 14 17 27 29 47 52 65
7,9,11, 14, 17, 27, 29, 47, 52, 65
#include<stdio.h>
void quicksort(int a[], int lb, int ub)
{
int j;
if(lb<ub)
{
j=partition(a,lb,ub);
quicksort(a,lb,j-1);
quicksort(a, j+1, ub);
}
}
int partition(int a[], int lb, int ub)
{
int i,j,pivot,temp;
i=lb+1;
j=ub;
pivot=a[lb];
do
{
while((a[i]<pivot)&&(i<=ub))
i++;
while((a[j]>pivot)&&j>lb))
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}while(i<j);
a[lb]=a[j];
a[j]=pivot;
return j;
}
• Quick Sort :
• Time Complexity :
• Best Case : Ο(nlogn).
• Worst Case : Ο(n2).
• Advantages :
1. It is very efficient sorting method.
2. No additional data structures is required.
3. It is in-place sort method.
• Disadvantages :
1. It is not stable sorting method.
Array as a Data Structure
Comparison Of Comparison Based Sorting Methods :
Bubble Sort Ο(n2) Ο(n2) Yes Yes η can be improved using modified
bubble sort.
Insertion Ο(n) Ο(n) Yes Yes Most suitable for sorted or almost
Sort sorted Lists.
Merge Sort Ο( nlogn ) Ο( nlogn) No Yes Requires additional Ο(n) space.
4.B. 1 2 3 4 5
2 2 3 5 5
A =2, 5, 3, 2, 1, 7, 1
MAX = 7
C ARRAY
SIZE = 1 2 3 4 5 6 7
COUNT 22 1 0 1 0 1
DECRE 2 10 43 54 5 6 6 76
B. 1 2 3 4 5 6 7
1 1 2 2 3 5 7
4, 3, 5, 6, 8,3, 4, 7, 4
MAX = 8
C Array
Size 1 2 3 4 5 6 7 8
Total 0 0 2 3 1 1 1 1
Count dec 0 0 2 10 5 4 3 6 5 76 87 98
B Array
Size 1 2 3 4 5 6 7 8 9
3 3 4 4 4 5 6 7 8
#include<stdio.h>
count(int a[], int n, int k)
{
int c[10],b[10],i,j;
For(i=1;i<=k;i++)
C[i]=0;
For(i=1;i<=n;i++)
{
c[a[i]]++;
}
for(i=2; i<=k;i++)
{
c[i]=c[i]+c[i-1];
}
for(i=n;i>=1;i--)
{
b[c[a[i]]=a[i];
c[a[i]=c[a[i]]-1;
}
for(i=1;i<=n;i++)
{
a[i]=b[i];
}
}
for(i=2;i<=n;i++)
{
If(a[i]>k)
K=a[i];
}
count(a,n,k);
printf(“\n The sorted list is”);
for(i=1;i<=n;i++)
{
printf(“%d”,a[i]);
}
}
Advantages :
1. Its complexity is n+k in all cases.
Disadvantages :
1. If non-primitive (object) elements are sorted, another helper array is
needed to store the sorted elements.
2. Counting sort can be used only to sort discrete values (for example
integers), because otherwise the array of frequencies cannot be
constructed.
Radix Sort :
It is a non-comparative sorting algorithm.
1.A method that can be used to sort a list of a number by its base. If we
want to sort the list of English words, where radix or base is 26 then 26
buckets are used to sort the words.
2.To sort an array of decimal number where the radix or base is 10 we
need 10 buckets and can be numbered as 0,1,2,3,4,5,6,7,8,9. A number
of passes required to have a sorted array depend upon the number of
digits in the largest element.
34, 76, 85, 48, 13, 10, 27, 25, 20
34= LSB= 4, MSB=3
1.Arrange by L.S.B
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
10, 20, 13, 34, 85, 25, 76, 27, 48
2. Arrange by M.S. B
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
10, 13, 20, 25, 27, 34, 48, 76, 85
22, 39, 45, 99, 67, 33, 08, 27, 12, 05
1.Arrange by L.S.B
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
22, 12, 33, 45, 05, 67, 27, 08, 39, 99
2. Arrange by M.S.B
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
05, 08, 12, 22, 27, 33, 39, 45, 67, 99
387, 124, 233, 299, 150, 264, 345, 825, 566, 239,120
1 bit(L.S.B) = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
150, 120, 233, 124, 264, 345, 825, 566, 387, 299, 239
2 bit = 0,1, 2, 3, 4, 5, 6, 7, ,8, 9
120, 124, 825, 233, 239, 345, 150, 264, 566, 387, 299
3 bit(M.S.B)= 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
120, 124, 150, 233, 239, 264, 299, 345, 387, 566, 825
Radix sort has also been called bucket sort and digital sort.
• Time Complexity :
• Best Case : Ο(n2)
• Worst Case : Ο(nlogn)
Radix Sort :
• Advantages :
• Fast when the keys are short i.e. when the range of the array
elements is less.
• It is implemented in Java, it would be faster than quicksort or heap.
• It is stable because it preserves existing order of equals keys.
• It is good on small keys.
• Disadvantages :
1. Since Radix Sort depends on digits or letters, Radix Sort is much less
flexible than other sorts. Hence , for every different type of data it
needs to be rewritten.
2. The constant for Radix sort is greater compared to other sorting
algorithms.
3. It takes more space compared to Quicksort which is inplace sorting.
Comparison Of Non Comparison Based Sorting Methods :
Special Input Condition Best Worst Space
Method Case Case Compexity