Sorting and Searching 5m
Sorting and Searching 5m
printf(“%d%d%d”,m1,m2,m3);
}
Classification
of Arrays
Two dimension array
10 15 20 25 30
• Since array is identical by common name any element can be accessed by specifying the subscription (or
an index). For example , in above array:
• 0th element 10 can be accessed by specifying A[0]
• 1st element 15 can be accessed by specifying A[1]
• 2nd element 20 can be accessed by specifying A[2]
• 3rd element 25 can be accessed by specifying A[3]
• 4th element 30 can be accessed by specifying A[4]
Dayananda Sagar Academy of Technology and Management
Basic properties of array:
• The array elements should be of the same data type.
• The data items are stored contiguously in memory.
• The subscript of first item is always zero.
• Each data item is accessed using the name of the array, but with
different subscript.
• Index of array is always an integer.
• Ex 1: a[1.5] // Error index has to be integer
• Ex 2: a[1] // OK
• Ex 3: a[‘5’] //OK index is an integer
// ‘5’ has an ASCII value which is integer.
• The size used during declaration of the array informs the compiler to allocate and reserve the specified
memory locations.
• Ex 2: float avg[5];
• Ex 3: char name[5];
• Where,
• type can be int, float, char ,double
• array_name is name of the array
• expression/size should be evaluated to integer.
• list of values are v1,v2,….vn should be enclosed with in ’{‘ and ‘}’ and separated by
commas.
During compilation, 5 contiguous memory locations are reserved by the compiler for the variable a and all
these locations are initialized as shown in figure.
10 15 20 25 30
10 15 0 0 0
C O M P U T E R /0
b[0] b[1] b[2] b[3] b[4] b[5] b[6] b[7] b[8]
• Even though the string "COMPUTER" contains 8 characters, because it is a string, it always ends with null
character. So, the array size is 9 bytes (i.e., string length 1 byte for null character).
• Ex:-
char b[9]="COMPUTER"; // correct
char b[8]="COMPUTER"; // wrong
Dayananda Sagar Academy of Technology and Management
Reading / Writing single dimension arrays
• We can easily read, write or process the array items using appropriate
programming constructs such as for-loop, while or do-while.
• Consider the declaration :
int a[5];
• Here, memory for 5 integer is reserved and each items in array can be
accessed by index and name.
Using a[0] through a[4] we can access 5 integers
Similarly to display n data items stored in the array, replace scanf() by printf()
statement as shown below:
for(i=0;i<n;i++)
{
printf(“%d”, a[i]);
}
Dayananda Sagar Academy of Technology and Management
Program to read n items from keyboard and display n elements on the monitor.
int main()
{
int i, a[100],n;
printf(“Enter the size of an array “);
scanf(“%d”, &n); // read array size
printf(“Enter %d elements\n”, n);
for(i=0;i<n; i++)
scanf(“%d”, &a[i] ); // to read array elements
printf(“Array elements are \n”);
for(i=0;i<n; i++)
printf(“%d”, a[i]); // to print array elements
}
Dayananda Sagar Academy of Technology and Management
Write a c-program to find sum of N-array
elements
printf("enter %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter %d elements of Array B\n",n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
c[i]=a[i]+b[i];
printf("addition of array A & B is \n");
for(i=0;i<n;i++)
printf("%d + %d=%d\n", a[i],b[i],c[i]);
}
Dayananda Sagar Academy of Technology and Management
Output:
Enter the size of array A
3
enter 3 elements
1
2
3
enter 3 elements of Array B
3
4
5
addition of array A & B is
1 + 3=4
2 + 4=6
3 + 5=8
Dayananda Sagar Academy of Technology and Management
Write a C program to generate and print first
– N Fibonacci series using array
•Linear Search
•Binary Search
Dayananda Sagar Academy of Technology and Management
Linear Search (LS)
Linear Search involves checking all the
elements of the array (or any other structure)
one by one and in sequence until the desired
result is found.
Found a match at
index 2
Enter the number of elements
5
Enter the 5 elements of array
34
2
76
56
90
Enter the key to search
56
Item found in position 4
• Disadvantages
– Time inefficient as compared to other
algorithms
– Not suitable for large-sized lists
– Search time increases with number of
elements
Binary Search (BS)
17-01-2020 40
Program 6
=(0+2)/2=2 50
a[0] a[1] a[2] a[3] a[4]
if(key==a[mid])
{ L=2+1=3 10 20 30 40 50
H=4
found=1;
m=(l+h)/2
break;
Low mid high
} =(3+4)/2=3
a[mid]=40
else if(key>a[mid]) 50
L=3+1=4 a[0] a[1] a[2] a[3] a[4]
low=mid+1; H=4
10 20 30 40 50
M=(4+4)/2=4
else
high=mid-1;
} Low high
mid
a[mid]=50
Dayananda Sagar Academy of Technology and Management
if(found ==1)
printf(“Item found in position : %d”,mid+1);
else
printf("\n Item not found\n");
}
30 10 60 20 50 40
Unsorted List
10 20 30 40 50 60
Sorted List
METHODS OF SORTING:
🞆 Bubble Sort
🞆 Selection Sort
BUBBLE SORT
ALGORITHM
🞆Bubble Sort:
Algorithm of bubble sort includes two steps
repeated until the list is sorted.
⮚ Compare adjacent elements, if the
element on right side is smaller then
swap their positions.
⮚ Compare first element, second element
and so on on completion of Pass 1 the
largest element is at last position.
50 10 30 20 40
50 10 30 20 40 0,1
10 50 30 20 40 1,2
10 30 50 20 40 2,3
10 30 20 50 40
3,4
10 30 20 40 50
Pass 2:
10 30 20 40 50 0,1
10 30 20 40 50
1,2
10 20 30 40 50
2,3
10 20 30 40 50
Pass 3:
10 20 30 40 50 0,1
10 20 30 40 50
1,2
10 20 30 40 50
Pass 4:
10 20 30 40 50 0,1
10 20 30 40 50
10 20 30 40 50 Sorted list
PROGRAM 11
Develop a program to sort the
given set of N numbers using
Bubble sort.
23 15 29 11 1
0,1
15 23 29 11 1
0,2
15 23 29 11 1 0,3
11 23 29 15 1
0,4
1 23 29 15 11
Pass 2:
1 23 29 15 11
1,2
1 23 29 15 11
1,3
1 15 29 23 11
1,4
1 11 29 23 15
Pass 3:
1 11 29 23 15
2,3
1 11 23 29 15
2,4
1 11 15 29 23
Pass 4:
1 11 15 29 23
3,4
1 11 15 23 29
1 11 15 23 29 Sorted List
/* program to sort the array
elements using selection sort */
(sum+3)x
(sum+7)x
(sum+6)x
(sum+3)x
Sum+9
Dayananda Sagar Academy of Technology and Management
Dayananda Sagar Academy of Technology and Management
# include<stdio.h>
# include<conio.h>
void main( )
{
int i, n, sum, x, a[20];
clrscr( );
printf("Enter the number :of coefficients\n") ;
scanf("%d", &n) ;
printf("Enter the Coefficients\n") ;
for (i = 0; i<=n; i ++)
{
scanf ("%d", &a[i]);
}
Dayananda Sagar Academy of Technology and Management
printf("Enter the value of x/n");
scanf("%d", &x);
sum=a[n]*x;
for (i=n-1;i>0;i--)
{
sum=(sum+a[i])*x;
}
sum=sum+a[0];
printf("The result=%d",sum);
getch ( ) ;
}