DATA STRUCTURE-ASSIGNMENT NO.2:
PRACTICE PROGRAMS:
1) Write a C program to create a integer array with elements {56,23,11,67,12,89,2} and sort
the given array using bubble sort.
ANS:
#include <stdio.h>
#include <conio.h>
int main()
{
int a[] = {56, 23, 11, 67, 12, 89, 2};
int i;
int temp, pass;
printf("\n the input array is: ");
for ( i = 0; i < 7; i++)
{
printf("\t%d",a[i]);
}
for (pass = 1; pass < 7; pass++)
{
for (i = 0; i < 7 - pass; i++)
{
if (a[i] > a[i + 1])
{
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
printf("\n the sorted array is:");
for ( i = 0; i < 7; i++)
{
printf("\t %d",a[i]);
}
}
---------------------------------------------------------------------------------------
2) Write a C program to sort a random array of n integers (value of n accepted from user) by
using Bubble Sort / Insertion Sort algorithm in ascending order.
ANS:
#include<stdio.h>
#include<conio.h>
int main(){
int a[10],i,n;
int pass,temp;
printf("\n enter array limit:");
scanf("%d",&n);
printf("\n enter array elements:");
for ( i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
for ( pass = 1; pass<n; pass++)
{
for ( i = 0; i < n-pass; i++)
{
if (a[i]>a[i+1])
{
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
printf("\n sorted array is:");
for ( i = 0; i < n; i++)
{
printf("\t %d",a[i]);
}
}
-----------------------------------------------------------------------------------
3) Write a C program to create a string array with 5 elements which contains word starting
with vowel and sort them using Selection sort.
ANS:
#include <stdio.h>
#include <string.h>
void selectionSort(char arr[][30], int n)
{
int i, j, min_idx;
char minStr[30];
for (i = 0; i < n - 1; i++)
{
int min_idx = i;
strcpy(minStr, arr[i]);
for (j = i + 1; j < n; j++)
{
if (strcmp(minStr, arr[j]) > 0)
{
strcpy(minStr, arr[j]);
min_idx = j;
}
}
if (min_idx != i)
{
char temp[30];
strcpy(temp, arr[i]);
strcpy(arr[i], arr[min_idx]);
strcpy(arr[min_idx], temp);
}
}
}
int main()
{
int n;
printf("\n enter size pf array:");
scanf("%d", &n);
int i;
char arr[n][30];
printf("\n enter array elements in string format:");
for (i = 0; i < n; i++)
{
printf("\n Enter [%d] index string:", i);
scanf("%s", arr[i]);
}
printf("\n Given array is:");
for (i = 0; i < n; i++)
{
printf("\t%s", arr[i]);
}
selectionSort(arr, n);
printf("\nSorted array is:");
for (i = 0; i < n; i++)
printf("\t%s", arr[i]);
return 0;
}
-----------------------------------------------------------------------------------
SET A:
1) Write a C program to accept and sort n elements in ascending order by using bubble sort.
ANS:
#include <stdio.h>
#include <conio.h>
int main()
{
int a[10], n, i;
int pass, temp;
printf("\n enter a limit:");
scanf("%d", &n);
printf("\n enter array elements:");
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for (pass = 1; pass < n - 1; pass++)
{
for (i = 0; i < n - pass; i++)
{
if (a[i] > a[i + 1])
{
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
printf("\n the sorted array is:");
for ( i = 0; i < n; i++)
{
printf("\t%d",a[i]);
}
}
-------------------------------------------------------------------------------
2) Write a C program to accept and sort n elements in ascending order by using insertion sort.
ANS:
#include<stdio.h>
#include<conio.h>
int main(){
int a[10],n,i,j,temp;
printf("\n enter array limit:");
scanf("%d",&n);
printf("\n enter array elements:");
for ( i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
for ( i = 1; i < n; i++)
{
temp=a[i];
for ( j = i-1; j>=0&&a[j]>temp; j--)
{
a[j+1]=a[j];
}
a[j+1]=temp;
}
printf("\n sorted array is:");
for ( i = 0; i < n; i++)
{
printf("\t%d",a[i]);
}
}
---------------------------------------------------------------------------------------------------------------
3) Write a ‘C’ program to accept and sort n elements in ascending order using Selection sort
method.
ANS:
#include<stdio.h>
#include<conio.h>
void selection_sort(int a[],int n){
int j,i,temp,temp1;
for ( i = 0; i < n; i++)
{
temp=a[i];
for ( j = i+1; j< n ; j++)
{
if (a[j]<temp)
{
temp1=temp;
temp=a[j];
a[j]=temp1;
}
}
a[i]=temp;
}
}
int main(){
int a[20],n,i;
printf("\n enter array limit:");
scanf("%d",&n);
printf("\n enter array elements:");
for ( i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
selection_sort(a,n);
printf("\n the sorted array is:");
for ( i = 0; i < n; i++)
{
printf("\t%d",a[i]);
}
}
-----------------------------------------------------------------------------------------------
SET B:
1) Write a C program to create a string array with day of week and sort them using Insertion
sort.
ANS:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define l 7
void display(char a[][20])
{
for (int i = 0; i < l; i++)
{
printf("\t%s", a[i]);
}
printf("\n");
}
int main()
{
char a[l][20] = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};
char temp[20];
int i, j;
/*printf("\n enter days of week:");
for (i = 0; i < l; i++)
{
scanf("%s", &a[i]);
}*/
printf("\n the original input array is:");
display(a);
for (i = 1; i < l; i++)
{
strcpy(temp, a[i]);
for (j = i - 1; j >= 0 && strcmp(a[j], temp) > 0; j--)
{
// if (strcmp(a[j], a[j - 1]) > 0)
{
strcpy(a[j + 1], a[j]);
}
}
strcpy(a[j + 1], temp);
}
printf("\n the sorted array is:");
display(a);
}
---------------------------------------------------------------------------------------------------------------------
2) Write a ‘C’ program to accept names from the user and sort in alphabetical order using
bubble sort.
ANS:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define l 6
void display(char array[][30])
{
for (int i = 0; i < l; i++)
{
printf("%s ", array[i]);
}
printf("\n");
printf("\n");
}
int main()
{
char array[l][30];
printf("Enter %d Strings: \n", l);
for (int i = 0; i < l; i++)
{
scanf("%s", array[i]);
}
printf("Original array: ");
display(array);
char temp[30];
for (int i = 0; i < l; i++)
{
for (int j = 0; j < i; j++)
{
if (strcmp(array[j], array[j + 1]) > 0)
{
strcpy(temp, array[j]);
strcpy(array[j], array[j + 1]);
strcpy(array[j + 1], temp);
}
}
}
printf("Sorted Array: ");
display(array);
}
-------------------------------------------------------------------------------------
3) Write a C program to accept and sort n elements in ascending order by using bubble sort
and also count the number of swaps. Display the sorted list and total no of swap count.
ANS:
#include <stdio.h>
#include <conio.h>
int main()
{
int a[10], i, n;
int pass, temp, count = 0;
printf("\n enter array limit:");
scanf("%d", &n);
printf("\n enter array elements:");
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for (pass = 1; pass < n ; pass++)
{
for (i = 0; i < n - pass; i++)
{
if (a[i] > a[i + 1])
{
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
count++;
}
}
}
printf("\n the sorted array is:");
for (i = 0; i < n; i++)
{
printf("\t%d", a[i]);
}
printf("\n the no.of swaps are %d", count);
}
-------------------------------------------------------------------------------------
SET C:
1) Write a C program to read the data from the file “employee.txt” which contains empno and
empname and sort the data on names alphabetically (use strcmp) using Bubble Sort.
ANS:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
void display(char e_name[][30], int size)
{
for (int i = 0; i < size; i++)
{
printf("\n%d:%s", i, e_name[i]);
}
}
void bubble_sort(char e_name[][30], int size)
{
char temp[30];
int pass, i;
for (pass = 1; pass < size - 1; pass++)
{
for (i = 0; i < size - pass; i++)
{
if (strcmp(e_name[i], e_name[i + 1]) > 0)
{
strcpy(temp, e_name[i]);
strcpy(e_name[i], e_name[i + 1]);
strcpy(e_name[i + 1], temp);
}
}
}
}
int main()
{
FILE *ptr;
int size;
char e_name[10][30];
int e_no[30];
int i;
ptr = fopen("emp.txt", "r");
if (ptr == NULL)
{
printf("\n the file does not exist!");
exit(0);
}
i = 0;
while (!feof(ptr))
{
fscanf(ptr, "%d%s", &e_no[i], &e_name[i]);
i++;
}
size = i;
bubble_sort(e_name, size);
printf("\n The sorted elements:");
display(e_name, size);
ptr = fopen("emp.txt", "a");
fprintf(ptr, "\n The sorted file is:\n");
for (i = 0; i < size; i++)
{
fprintf(ptr, "\n%d %s", i, e_name[i]);
}
// bubble_sort( struct file e_no, struct file e_name);
fclose(ptr);
}
TEXT FILE ARE:
1 ganesh
2 ganpat
3 raghunandan
4 ram
5 krishna
6 rohan
7 rohit
8 rakesh
9 ramesh
----------------------------------------------------------------------------------------
2) Write a C program to read the data from the file “person.txt” which contains personno and
personage and sort the data on age in ascending order using insertion Sort / Selection Sort.
ANS:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void insertion_sort(int a[],int n){
int temp,j,i;
for ( i = 0; i < n; i++)
{
temp=a[i];
for ( j = i-1; j>=0&&temp<a[j]; j--)
{
a[j+1]=a[j];
}
a[j+1]=temp;
}
}
void display(int a[],int n){
for (int i = 0; i < n; i++)
{
printf("\t%d",a[i]);
}
}
int main(){
FILE *ptr;
int i,size=0;
int p_no[20],p_age[20];
ptr=fopen("person.txt","r");
if (ptr==NULL)
{
printf("\n The file does not exist!");
exit(0);
}
for ( i = 0; !feof(ptr); i++)
{
fscanf(ptr,"%d%d",&p_no[i],&p_age[i]);
size++;
}
insertion_sort(p_age,size);
printf("\n The sorted elements is:");
display(p_age,size);
ptr=fopen("person.txt","a");
fprintf(ptr,"\n The age wise sorted data is:\n");
for ( i = 0; i < size; i++)
{
fprintf(ptr,"\n%d %d",p_no[i],p_age[i]);
}
fclose(ptr);
getch();
return 0;
}
TEXT FILE ARE:
1 56
2 43
3 67
4 23
5 25
6 60
7 87
--------------------------------------------------------------------------------------------
3) Modify the bubble sort, insertion sort and selection sort program of Set A to sort the
integers in descending order?
ANS:
BUBBLE SORT:
/*sort data using bubble sort in descending order!*/
#include <stdio.h>
#include <conio.h>
void display(int a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("\t%d", a[i]);
}
}
void bubble_sort(int a[], int n)
{
int temp, pass, i;
for (pass = 1; pass < n - 1; pass++)
{
for (i = 0; i < n - pass; i++)
{
if (a[i] < a[i + 1])
{
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
}
int main()
{
int a[20], i, n;
printf("\n Enter array limit:");
scanf("%d", &n);
printf("\n Enter array elements:");
for (i = 0; i < n; i++)
{
printf("\nEnter[i]index element:");
scanf("%d", &a[i]);
}
printf("\n The original array is:");
display(a, n);
bubble_sort(a, n);
printf("\n The sorted array by descending order:\n");
display(a,n);
}
INSERTION SORT:
/*sort an array elements in descending order using insertion sort*/
#include <stdio.h>
#include <conio.h>
void display(int a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("\t%d", a[i]);
}
}
void insertion_sort(int a[],int n){
int i,temp,j;
for ( i = 0; i < n; i++)
{
temp=a[i];
for ( j = i-1; j>=0&&a[j]<temp; j--)
{
a[j+1]=a[j];
}
a[j+1]=temp;
}
}
int main()
{
int a[20], i, n;
printf("\n Enter array limit:");
scanf("%d", &n);
printf("\n Enter array elements:");
for (i = 0; i < n; i++)
{
printf("\nEnter [i] index element:");
scanf("%d", &a[i]);
}
printf("\n The original array is:");
display(a, n);
insertion_sort(a,n);
printf("\nThe sorted array by descending order is:\n");
display(a,n);
}
SELECTION SORT:
/*sort array using selection sort in descending order!*/
#include<stdio.h>
#include<conio.h>
void selection_sort(int a[],int n){
int j,i,temp,temp1;
for ( i = 0; i < n; i++)
{
temp=a[i];
for ( j = i+1; j< n ; j++)
{
if (a[j]>temp)
{
temp1=temp;
temp=a[j];
a[j]=temp1;
}
}
a[i]=temp;
}
}
int main(){
int a[20],n,i;
printf("\n enter array limit:");
scanf("%d",&n);
printf("\n enter array elements:");
for ( i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
selection_sort(a,n);
printf("\n the sorted array is:");
for ( i = 0; i < n; i++)
{
printf("\t%d",a[i]);
}
}
---------------------------------------------------------------------------------------------------------------
Comments
Post a Comment
Enter comment here!