In this video tutorial we shall see how we can search for a number in an array in linear fashion.
First ask the user to enter the number to be searched. Store it inside a variable called key. Now start comparing key value with a[i] using a for loop. If the key is found in the array, then assign 1 to flag orelse flag will be 0.
Depending upon the value of flag, print the message.
If flag is 1, search is successful. If flag is 0, search failed. in the sense, the number is not present in the given array.
Now, if a[i] contains a number which is bigger than fbig, we transfer the value of a[i] to fbig and the value present in fbig to sbig; If the value of a[i] is not bigger than fbig, then we check it with sbig. If a[i] is bigger than sbig, then we assign the value of a[i] to sbig.
This way, at the end of the loop fbig will have the first biggest and sbig will have the second biggest element/number in the array.
Video Tutorial: Find First and Second Biggest In An Array, Without Sorting It: C
First we have to assume that a[0] is biggest. Now store the value of a[0] inside a variable called big. Now compare value of big with other values in the array. If big is less than any other value, then store the bigger value inside variable big.
Video Tutorial: Biggest In An Array, Without Sorting It: C
Note that, the previous value inside the variable big is discarded.
Full source code
#include
#include
void main()
{
int big, a[20], N, pos, i;
clrscr();
printf("Enter the array size\n");
scanf("%d", &N);
printf("Enter %d elements\n", N);
for(i = 0; i big)
{
big = a[i];
pos = i+1;
}
printf("Big is %d and its position is %d", big, pos);
getch();
}
Output Enter the array size 5 Enter 5 elements 2 0 100 108 55 Big is 108 and its position is 4
In this video tutorial we illustrate the bubble sort algorithm and also see how to write it in C programming, for 5 elements as well as to N elements.
In bubble sort, a[0] is compared with a[1]. a[1] with a[2], a[2] with a[3] .. so on if a[0] is greater than a[1], the values are swapped. This logic continues till the end of the array.
At the end of each iteration, 1 element gets sorted.
If the array size is 5, then we will have 4 iterations. i.e., if size is N, then we will have N-1 iterations.
Selection sort is a sorting technique which is inefficient on large list of elements. Its simple and easy to understand.
In selection sort, first we select one element and compare it with the rest of the elements. If the compared element is smaller than the selected element, then those numbers are sorted. This approach is continued till the last element in the array index.
The number of iterations is 1 less than the array size. i.e., if the user enters array size as 5, then there will be 4 iterations.
Below is an example:
Lets assume, user has entered the array size as 5. and entered 13, 0, -2, 8, 3 as array elements.
First Iteration:
In first iteration, a[0] is selected. and is compared with a[1], a[2], a[3] and a[4]. and is swapped with the least value in the array.
Second Iteration:
In the second iteration, we leave off a[0] and select a[1] and compare it with the rest of the elements in the array i.e., a[2], a[3] and a[4]. and swap the least value with a[1]
Third Iteration:
In third iteration, we select a[2] and compare it with a[3], a[4] and swap the least value to a[2]
Forth Iteration:
In forth iteration, we select a[3] and compare it with a[4] and swap the least value.
At the end of 4 iterations all array elements are sorted.
Since the array index starts from 0, and we need not compare the last element in the array with any other elements, we iterate the outer loop from 0 to N-2. We iterate the inner loop from i+1 till N-1.
Source Code: Selection Sort of N numbers: C
#include
#include
void main()
{
int a[20], i, j, N, temp;
clrscr();
printf("Enter the size of the array\n");
scanf("%d", &N);
printf("Enter %d elements\n", N);
for(i=0; i a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
printf("\nSorted Array:\n");
for(i=0; i
Output:
Enter the size of the array
5
Enter 5 elements
13
0
-2
8
3
Sorted Array:
-2
0
3
8
13