Sorting
Sorting
Sorting
Example:
#include <stdio.h>
#include <conio.h>
int main()
{
clrscr();
int n = 10;
int a[] = {3,2,6,5,4,7,8,9,5,1};
int min_index;
for(int i = 0; i < n - 1; i++)
{
min_index = i;
for(int j = i + 1; j < n; j++)
{
if(a[min_index] > a[j])
{
min_index = j;
}
}
if(min_index != i)
{
int temp = a[i];
a[i] = a[min_index];
a[min_index] = temp;
}
}
printf("Sorted Array: ");
for( i = 0; i < n; i++)
{
printf(" %d", a[i]);
}
Bubble Sort
In Bubble sort in C, we compare two adjacent Example:
elements of an array to find which one is greater or lesser #include<stdio.h>
and swap them based on the given condition, whether #include<conio.h>
ascending or descending, until the final place of the void main()
element is not found. {
We repeat this until no more swaps are required, clrscr();
and all the elements get sorted. Bubble sort gets its name int a[20],i,j,n,temp;
from the fact that it filters out the elements at the top of printf("Enter the no. of element to be sorted: ");
the array-like bubbles on water. scanf("%d",&n);
To understand it in a better way, let's illustrate it using a for(i=0;i<n;i++)
step-by-step method: {
Assuming we want to sort an array in ascending order and printf("\n Enter the no. %d:",i+1);
let’s name it arr with n elements in it. Now, this is how the scanf("%d",&a[i]);
Bubble sort algorithm in C will work: }
STEP 1: for(i=0;i<n-1;i++)
Starting at index zero, compare the elements with {
their next ones (arr[0] & arr[1]), and swap if arr[0]>arr[1]. for(j=0;j<n-1-i;j++)
After that compare arr[1] & arr[2] and swap if {
arr[1]>arr[2]. Repeat this process until the end of the if(a[j]>a[j+1])
array. {
After that, the largest element of the array will be temp=a[j];
present at the end index. This is known as the first pass. a[j]=a[j+1];
We have processed the array elements from [0:n−1] in a[j+1]=temp;
this first pass. }
STEP 2: }
Repeat STEP 1 but process array elements }
from [0:n−2] because the last element arr[n-1] is already printf("\n The array of element after sorting is:");
present at its correct position. After this, the largest two for(i=0;i<n;i++)
elements are present at the end. {
Basically, for an ith pass, we will traverse array printf("\t%d",a[i]);
elements till (n-i)th index because i elements from the last }
are already sorted in their places. getch();
STEP 3: }
Repeat this process n-1 times to finally get the
sorted array in the end. As we are getting the largest
respective element at the (n - i)th index each time, there
is no need to iterate through the array for n th pass as the
first element will be the smallest element of the array
already placed.
For instance, if we pass an array consisting of the
elements: (5, 3, 8, 4, 6), then the final array after the
bubble sort implementation will be: (3, 4, 5, 6, 8).
Insertion Sort #include<stdio.h>
#include<conio.h>
Insertion sort is an algorithm to sort the given void main()
array. One by one it picks elements of an array from the {
beginning and sorts the array by placing them in their clrscr();
right position. It can be implemented using for loop, while int a[20],i,j,n,temp;
loop, and swapping functions. printf("Enter the no. of element to be sorted: ");
scanf("%d",&n);
for(i=0;i<n;i++)
Step 1 − If the element is the first one, it is already sorted. {
printf("\n Enter the no. %d:",i+1);
Step 2 – Move to next element
scanf("%d",&a[i]);
Step 3 − Compare the current element with all elements in }
the sorted array for(i=0;i<n;i++)
{
Step 4 – If the element in the sorted array is smaller than
temp=a[i];
the current element, iterate to the next element.
j=i-1;
Otherwise, shift all the greater element in the array by
while (j>=0 && a[j]>temp)
one position towards right
{
Step 5 − Insert the value at the correct position a[j+1]=a[j];
j=j-1;
Step 6 − Repeat until the complete list is sorted
}
a[j+1]=temp;
}
printf("\n The array of element after sorting is:");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
getch();
}
Insert a no. in an array
#include<stdio.h>
#include<conio.h>
void main()
{
Clrscr();
int a[20],n,num,pos,i,k;
printf("Enter the no. of element is :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the no. for position %d in
an array: ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
printf("\n At %d position the element is
%d",i+1,a[i]);
}
printf("\n Enter the no you want to insert: ");
scanf("%d",&num);
printf("\n Enter the position of insertion: ");
scanf("%d",&pos);
n++;
for(k=n;k>=pos;k--)
{
a[k]=a[k-1];
}
--pos;
a[pos]=num;
printf("\n array after insertion is:");
for(i=0;i<n;i++)
{
printf("\t %d",a[i]);
}
getch();
}
WACP for merging of 2 sorted arrays into a 3rd sorted {
array? array3[k] = array1[i];
#include <stdio.h> i++;
#include <conio.h> k++;
int main() }
{ }
clrscr();
int array1[50], array2[50], array3[100], m, n, i, j, k = 0; printf("\n After merging: \n");
printf("\n Enter size of array Array 1: "); for (i = 0; i < m + n; i++)
scanf("%d", &m); {
printf("\n%d", array3[i]);
printf("\n Enter sorted elements of array 1: \n"); }
for (i = 0; i < m; i++) getch();
{ return 0;
scanf("%d", &array1[i]); }
} Or
printf("\n Enter size of array 2: ");
scanf("%d", &n); #include<stdio.h>
printf("\n Enter sorted elements of array 2: \n"); #include<conio.h>
for (i = 0; i < n; i++) int main()
{ {
scanf("%d", &array2[i]); clrscr();
} int a[100],b[10],n,m,i;
printf("Enter the no. of element for first array: ");
i = 0; scanf("%d", &n);
j = 0; for(i=0;i<n;i++)
while (i < m && j < n) {
{ printf("\n Enter the no. for
if (array1[i] < array2[j]) position %d in the first array: ",i+1);
{ scanf("%d",&a[i]);
array3[k] = array1[i]; }
i++; printf("Enter the no. of element for second
} array :");
else scanf("%d", &m);
{ for(i=0;i<m; i++)
array3[k] = array2[j]; {
j++; printf("\n Enter the no. for
} position %d in the second array: ",i+1);
k++; scanf("%d", &b[i]);
} }
n=n+m;
if (i >= m) for (i=0;i<m; i++)
{ {
while (j < n) a[(n-m)+i]=b[i];
{ }
array3[k] = array2[j]; printf("\n The numbers are after mergeing: ");
j++; for(i=0;i<n;i++)
k++; {
} printf("\t%d",a[i]);
} }
if (j >= n) getch();
{ return 0;
while (i < m) }
TWO DIMENSIONAL ARRAY for (i=0; i < 3; i++)
The syntax of declaration of 2-D array is similar to that of {
1-D arrays, but here we have 2 subscripts ( [row][col]). for (j=0; j< 3; j++)
e.g. int arr [4] [5]; {
data_type array_name [row size] [column size] c[i][j] = a[i][j] + b[i][j];
Q. WACP which initializes the sales amount of 3 }
salesman for 3 cities & display the values? }
#include <stdio.h> printf("\n the addition of matrix 1 and 2 is :");
#include <conio.h> for (i=0; i< 3; i++)
void main() {
{ for (j=0; j < 3; j++)
int i,j,sales[3][3]= {{10, 20, 30}, {40, 50, 60}, {70, 80, 90}}; {
clrscr(); printf("\t%d", c[i][j]);
printf ("the elements are: \n"); }
for (i=0; i< 3; i++) printf ("\n");
{ }
for (j=0; j<3; j++) getche();
{ }
printf ("\t%d", sales[i][j]); Or
} #include <stdio.h>
printf("\n"); #include <conio.h>
} int main() {
getche(); clrscr();
} int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100):
OUT PUT - ");
10 20 30 scanf("%d", &r);
40 50 60 printf("Enter the number of columns (between 1 and
70 80 90 100): ");
Q. WACP which accepts 2 matrices and display the sum scanf("%d", &c);
of 2 matrices?
#include <stdio.h> printf("\nEnter elements of 1st matrix:\n");
#include <conio.h> for (i = 0; i < r; ++i)
void main() for (j = 0; j < c; ++j) {
{ printf("Enter element a%d%d: ", i + 1, j + 1);
int a[3][3], b[3][3], c[3][3], i, j; scanf("%d", &a[i][j]);
clrscr(); }
printf ("enter the elements of the 1st matrix :");
for (i=0; i < 3; i++) printf("Enter elements of 2nd matrix:\n");
{ for (i = 0; i < r; ++i)
for (j=0; j<3; j++) for (j = 0; j < c; ++j) {
{ printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]); scanf("%d", &b[i][j]);
} }
}
printf("\n enter the elements of 2nd matrix:"); // adding two matrices
for (i=0, i < 3; i++) for (i = 0; i < r; ++i)
{ for (j = 0; j < c; ++j) {
for (j=0; j< 3; j++) sum[i][j] = a[i][j] + b[i][j];
{ }
scanf("%d", &b[i][j]);
} // printing the result
}
printf("\nSum of two matrices: \n"); {
for (i = 0; i < r; ++i) for (j = 0; j < r; ++j)
for (j = 0; j < c; ++j) { {
printf("%d ", sum[i][j]); b[i][j]=a[j][i];
if (j == c - 1) { }
printf("\n\n"); }
}
} printf("\n The transpose of given array is:\n");
Getche(); for (i = 0; i < c; ++i)
return 0; {
} for (j = 0; j < r; ++j)
Transpose of a Matrix {
When the rows of a matrix becomes columns and the printf("%d\t", b[i][j]);
column becomes rows, the process is called transpose of a }
matrix. printf("\n");
1 2 4 1 5 8 }
5 6 7 = 2 6 9 getch();
8 9 3 4 7 3 return 0;
(MATRIX) (TRANSPOSE OF MATRIX) }
WACP to find the transpose of a matrix?
#include <stdio.h>
#include <conio.h>
int main() {
clrscr();
int r,c,a[100][100],b[100][100],i,j;
printf("Enter the number of rows (between 1 and 100):
");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and
100): ");
scanf("%d", &c);