Assignment 2 Sorting Algorithms (Non Recursive) – Bubble Sort, Insertion Sort, Selection Sort
Set A
a) Sort a random array of n integers (accept the value of n from user) in ascending order by using bubble sort algorithm.
Program
#include <stdio.h>
#define MAXSIZE 10
void main()
{
int array[MAXSIZE];
int i, j, num, temp;
printf("Enter the value of num \n");
scanf("%d", &num);
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array is \n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
for (i = 0; i < num; i++)
{
for (j = 0; j < (num - i - 1); j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printf("Sorted array is...\n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
}
b) Sort a random array of n integers (create a random array of n integers) in ascending order by using insertion sort algorithm.
Program
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++) {
scanf("%d", &array[c]);
}
for (c = 1 ; c <= n - 1; c++) {
d = c;
while ( d > 0 && array[d-1] > array[d]) {
t = array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c <= n - 1; c++) {
printf("%d\n", array[c]);
}
return 0;
}
c) Sort a random array of n integers (accept the value of n from user) in ascending order by using selection sort algorithm.
#include <stdio.h>
void selection_sort();
int a[30], n;
void main()
{
int i;
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter elements of an array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
selection_sort();
printf("\n\nAfter sorting:\n");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
}
void selection_sort()
{
int i, j, min, temp;
for (i=0; i<n; i++)
{
min = i;
for (j=i+1; j<n; j++)
{
if (a[j] < a[min])
min = j;
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
Set B
a) Read the data from “Student.txt (Stud_name, Age, Percentage)” file and sort on Stud_name using bubble sort.(Create a separate sorted file).
Student.txt (Input File)
Amit 20 78.5
Neha 19 88.0
Rahul 21 72.5
Priya 20 91.5
Karan 22 68.0
#include <stdio.h> #include <stdlib.h> #include <string.h> struct Student { char Stud_name[30]; int Age; float Percentage; }; int main() { struct Student s[100], temp; int n = 0, i, j; FILE *fp, *fs; fp = fopen("Student.txt", "r"); if (fp == NULL) { printf("Unable to open Student.txt\n"); return 1; } // Read records from file while (fscanf(fp, "%s %d %f", s[n].Stud_name, &s[n].Age, &s[n].Percentage) != EOF) { n++; } fclose(fp); // Bubble Sort on Stud_name for (i = 0; i < n - 1; i++) { for (j = 0; j < n - i - 1; j++) { if (strcmp(s[j].Stud_name, s[j + 1].Stud_name) > 0) { temp = s[j]; s[j] = s[j + 1]; s[j + 1] = temp; } } } // Create Sorted File fs = fopen("SortedStudent.txt", "w"); if (fs == NULL) { printf("Unable to create SortedStudent.txt\n"); return 1; } fprintf(fs, "Name\tAge\tPercentage\n"); for (i = 0; i < n; i++) { fprintf(fs, "%s\t%d\t%.2f\n", s[i].Stud_name, s[i].Age, s[i].Percentage); } fclose(fs); printf("Records sorted successfully.\n"); printf("Sorted data stored in SortedStudent.txt\n"); return 0; }
b) Read the data from “Student.txt (Stud_name, Age, Percentage)” file and sort on Age using insertion sort. (Create a separate sorted file).
Student.txt (Input File)
Amit 20 78.5
Neha 19 88.0
Rahul 21 72.5
Priya 20 91.5
Karan 22 68.0
#include <stdio.h> #include <stdlib.h> struct Student { char Stud_name[30]; int Age; float Percentage; }; int main() { struct Student s[100], key; int n = 0, i, j; FILE *fp, *fs; fp = fopen("Student.txt", "r"); if (fp == NULL) { printf("Unable to open Student.txt\n"); return 1; } // Read data from file while (fscanf(fp, "%s %d %f", s[n].Stud_name, &s[n].Age, &s[n].Percentage) != EOF) { n++; } fclose(fp); // Insertion Sort on Age for (i = 1; i < n; i++) { key = s[i]; j = i - 1; while (j >= 0 && s[j].Age > key.Age) { s[j + 1] = s[j]; j--; } s[j + 1] = key; } // Create sorted file fs = fopen("SortedStudent.txt", "w"); if (fs == NULL) { printf("Unable to create SortedStudent.txt\n"); return 1; } fprintf(fs, "Name\tAge\tPercentage\n"); for (i = 0; i < n; i++) { fprintf(fs, "%s\t%d\t%.2f\n", s[i].Stud_name, s[i].Age, s[i].Percentage); } fclose(fs); printf("Records sorted successfully.\n"); printf("Sorted data stored in SortedStudent.txt\n"); return 0; }
c) Read the data from “Student.txt (Stud_name, Age, Percentage)” file and sort on Percentage using selection sort.(Create a separate sorted file).
Student.txt (Input File)
Amit 20 78.5
Neha 19 88.0
Rahul 21 72.5
Priya 20 91.5
Karan 22 68.0
#include <stdio.h> #include <stdlib.h> struct Student { char Stud_name[30]; int Age; float Percentage; }; int main() { struct Student s[100], temp; int n = 0, i, j, min; FILE *fp, *fs; fp = fopen("Student.txt", "r"); if (fp == NULL) { printf("Unable to open Student.txt\n"); return 1; } // Read data from file while (fscanf(fp, "%s %d %f", s[n].Stud_name, &s[n].Age, &s[n].Percentage) != EOF) { n++; } fclose(fp); // Selection Sort on Percentage for (i = 0; i < n - 1; i++) { min = i; for (j = i + 1; j < n; j++) { if (s[j].Percentage < s[min].Percentage) min = j; } if (min != i) { temp = s[i]; s[i] = s[min]; s[min] = temp; } } // Create sorted file fs = fopen("SortedStudent.txt", "w"); if (fs == NULL) { printf("Unable to create SortedStudent.txt\n"); return 1; } fprintf(fs, "Name\tAge\tPercentage\n"); for (i = 0; i < n; i++) { fprintf(fs, "%s\t%d\t%.2f\n", s[i].Stud_name, s[i].Age, s[i].Percentage); } fclose(fs); printf("Records sorted successfully.\n"); printf("Sorted data stored in SortedStudent.txt\n"); return 0; }
No comments:
Post a Comment