Sunteți pe pagina 1din 14

CSIT Assignment 2

Name: Rishabh Jain


Enrollment No: A1004919051
Course Code: CSIT124
Course Name: Data Structures using C
nd
B.Sc.(IT) 2 Semester
1. WAP to implement Quick Sort on 1D array of Student structure (contains student_name,
student_roll_no, total_marks), with key as student_roll_no. And count the number of swap
performed.
#include<stdio.h>
int count=0;
struct Student
{
char student_name[20];
int student_roll_no;
int total_marks;
}S[10], temp;
void swap(int a, int b)
{
temp=S[a];
S[a]=S[b];
S[b]=temp;
}
int partition(int low, int high)
{
int pivot=S[high].student_roll_no;
int i=low-1;
for(int j=low;j<high;j++)
{
if(S[j].student_roll_no<pivot)
{
i++;
swap(&S[i], &S[j]);
count++;
}
}
swap(&S[i+1], &S[high]);
count++;
return(i+1);
}
void quicksort(int low, int high)
{
if(low<high)
{
int pi=partition(low, high);
quicksort(low, pi-1);
quicksort(pi+1, high);
}
}
int main()
{
int i, n;
printf(“Enter no. of students: ”);
scanf(“%d”, &n);
printf(“Enter information of students:\n”);
for(i=0;i<n;i++)
{
printf(“Enter name of student: “);
scanf(“%s”, &S[i].student_name);
printf(“Enter student roll no: “);
scanf(“%d”, &S[i].student_roll_no);
printf(“Enter student marks: “);
scanf(“%d”, &S[i].total_marks);
}
quicksort(0, n-1);
printf(“\nNo. of swaps performed=%d\n”, count);
printf(“Displaying sorted students information according to roll no:\n”);
for(i=0;i<n;i++)
{
printf(“\nStudent name: %s”, S[i].student_name);
printf(“\nStudent roll no: %d”, S[i].student_roll_no);
printf(“\nStudent marks: %d”, S[i].total_marks);
}
return 0;
}

Output:
Enter no. of students: 4
Enter information of students:
Enter name of student: Swastik
Enter student roll no: 3
Enter student marks: 95
Enter name of student: Anshul
Enter student roll no: 2
Enter student marks: 90
Enter name of student: Samaksh
Enter student roll no: 4
Enter student marks: 85
Enter name of student: Aakarsh
Enter student roll no: 1
Enter student marks: 90

No. of swaps performed=3


Displaying sorted students information according to roll no:

Student name: Aakarsh


Student roll no: 1
Student marks: 90

Student name: Anshul


Student roll no: 2
Student marks: 90

Student name: Swastik


Student roll no: 3
Student marks: 95

Student name: Samaksh


Student roll no: 4
Student marks: 85
2. Write a program to implement Stack using array, also show overflow and underflow in respective
push and pop operations.
#include<stdio.h>
void push();
void pop();
void display();
int stack[100], top=-1, n;
int main()
{
int choice;

top=-1;
printf("Enter the size of STACK[MAX=100]: ");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\nEnter the Choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("\tEXIT POINT ");
break;
default:
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}while(choice!=4);
return 0;
}
void push()
{
int x;
if(top>=n-1)
printf("\n\tSTACK is over flow");
else
{
printf("Enter a value to be pushed: ");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
printf("\n\t Stack is under flow");
else
{
printf("\tThe popped elements is %d",stack[top]);
top--;
}
}
void display()
{
int i;
if(top>=0)
{
printf("\nThe elements in STACK");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\nPress Next Choice");
}
else
{
printf("\n The STACK is empty");
}

Output:
Enter the size of STACK[MAX=100]: 10
STACK OPERATIONS USING ARRAY
----------------------------------------------
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the Choice: 1
Enter a value to be pushed: 12

Enter the Choice: 1


Enter a value to be pushed: 24

Enter the Choice: 1


Enter a value to be pushed: 98

Enter the Choice: 3

The elements in STACK


98
24
12
Press Next Choice
Enter the Choice: 2
The popped elements is 98
Enter the Choice: 3

The elements in STACK


24
12
Press Next Choice
Enter the Choice: 4
EXIT POINT
3. Write a program to do the following sorting algorithm depending on user choice:
a. Selection sort (Done in Assignment – I)
b. Bubble sort (Done in Assignment – I)
c. Insertion sort (Done in Assignment – I)
d. Radix sort
e. Quick sort
f. Heap sort
g. Shell sort (Optional)
h. Tournament sort (Optional)
i. Merge sort
j. Tree sort (Optional)
k. Counting sort
#include<stdio.h>
void swap(int *a, int *b)
{
int temp= *a;
*a= *b;
*b= temp;
}
int getMax(int arr[], int n)
{
int mx = arr[0];
     for (int i = 1; i < n; i++)
         if (arr[i] > mx)
             mx = arr[i];
     return mx;
}
void countSort(int arr[], int n, int exp)
{
     int output[n];
     int i, count[10] = {0};
  
     for (i = 0; i < n; i++)
         count[ (arr[i]/exp)%10 ]++;
  
     for (i = 1; i < 10; i++)
         count[i] += count[i - 1];
  
         for (i = n - 1; i >= 0; i--)
     {
         output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
         count[ (arr[i]/exp)%10 ]--;
     }
  
     for (i = 0; i < n; i++)
         arr[i] = output[i];
}
int partition (int arr[], int low, int high)
{
     int pivot = arr[high];   
     int i = (low - 1); 
  
     for (int j = low; j <= high- 1; j++)
     {
         if (arr[j] < pivot)
         {
             i++;   
             swap(&arr[i], &arr[j]);
         }
     }
     swap(&arr[i + 1], &arr[high]);
     return (i + 1);
}
void quickSort(int arr[], int low, int high)
{
     if (low < high)
     {
         int pi = partition(arr, low, high);
  
         quickSort(arr, low, pi - 1);
         quickSort(arr, pi + 1, high);
     }
}
void heapify(int arr[], int n, int i)
{
int largest = i;
int l = 2*i + 1;
int r = 2*i + 2;

if (l < n && arr[l] > arr[largest])


largest = l;

if (right < n && arr[r] > arr[largest])


largest = r;

if (largest != i)
{
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
void mergeSort(int arr[], int l, int r)
{
     if (l < r)
     {
         int m = l+(r-l)/2;
  
         mergeSort(arr, l, m);
         mergeSort(arr, m+1, r);
  
         merge(arr, l, m, r);
     }
}
void merge(int arr[], int l, int m, int r)
{
     int i, j, k;
     int n1 = m - l + 1;
     int n2 =  r - m;
  
     int L[n1], R[n2];
  
     for (i = 0; i < n1; i++)
         L[i] = arr[l + i];
     for (j = 0; j < n2; j++)
         R[j] = arr[m + 1+ j];
  
     i = 0; // Initial index of first subarray
     j = 0; // Initial index of second subarray
     k = l; // Initial index of merged subarray
     while (i < n1 && j < n2)
     {
         if (L[i] <= R[j])
         {
             arr[k] = L[i];
             i++;
         }
         else
         {
             arr[k] = R[j];
             j++;
         }
         k++;
     }
   while (i < n1)
     {
         arr[k] = L[i];
         i++;
         k++;
     }
   while (j < n2)
     {
         arr[k] = R[j];
         j++;
         k++;
     }
}
int main()
{
int array[100], choice, i, j, pos, temp, flag=0;
printf(“Enter no. of elements: “);
scanf(“%d”, &n);
printf(“ Enter %d integers:\n”, n);
for(i=0; i<n; i++)
scanf(“%d”, &array[i]);
printf(“1-Selection Sort\n2-Bubble Sort\n3-Insertion Sort\n4-Radix Sort\n5-Quick Sort\n6-
Heap Sort\n7-Shell Sort\n8-Merge Sort\n9-Counting Sort\n”);
printf(“Enter choice: );
scanf(“%d”, &choice);
switch(choice)
{
case 1:
for(i=0; i<n-1; i++)
{
pos=i;
for(j=i+1; j<n; j++)
if(array[pos]>array[j])
pos=d
if(pos!=i)
swap(&array[i], &array[pos]);
}
printf(“Array sorted using Selection Sort is:\n”);
break;
case 2:
for(i=0; i<n-1; i++)
for(j=0; j<n-i-1; i++)
if(array[j]>array[j+1])
swap(&array[j], &array[j+1]);
printf(“Array sorted using Bubble Sort is:\n”);
break;
case 3:
for(i=1; i<n; i++)
{
temp=array[i];
for(j=i-1; j>=0; j--)
if(array[j]>temp)
{
array[j+1]=array[j];
flag=1;
}
else
break;
if(flag)
array[j+1]=temp;
}
printf(“Array sorted using Insertion Sort is:\n”);
break;
case 4:
int m = getMax(arr, n);
for (int exp = 1; m/exp > 0; exp *= 10)
         countSort(array, n, exp);
printf(“Array sorted using Radix Sort is:\n”);
break;
case 5:
quickSort(array, 0, n-1);
printf(“Array sorted using Quick Sort is:\n”);
break;
case 6:
for (i=n/2-1; i>=0; i--)
heapify(array, n, i);
for (i=n-1; i>=0; i--)
{
swap(array[0], array[i]);
heapify(array, i, 0);
}
printf(“Array sorted using Heap Sort is:\n”);
break;
case 7:
for (int gap = n/2; gap > 0; gap /= 2)
     for (i = gap; i < n; i ++)
         {
             temp = array[i];
           
             for (j = i; j >= gap && array[j - gap] > temp; j -= gap)
                 array[j] = array[j - gap];
              
             array[j] = temp;
         }
printf(“Array sorted using Shell Sort is:\n”);
break;
case 8:
mergeSort(array, 0, n - 1);
printf(“Array sorted using Merge Sort is:\n”);
break;
case 9:
int output[10];
int max = array[0];
for ( i = 1; i < size; i++)
if (array[i] > max)
max = array[i];
int count[10];
for ( i = 0; i <= max; ++i)
count[i] = 0;
for ( i = 0; i < size; i++)
count[array[i]]++;
for ( i = 1; i <= max; i++)
count[i] += count[i - 1];
for ( i = size - 1; i >= 0; i--)
{
output[count[array[i]] - 1] = array[i];
count[array[i]]--;
}
for ( i = 0; i < size; i++)
{
array[i] = output[i];
}
printf(“Array sorted using Counting Sort is:\n”);
break;
default:
printf ("\n\t Please Enter a Valid Choice");
}
for(i=0;i<n;i++)
printf(“%d ”, array[i]);
return 0;
}

Output:
Enter no. of elements: 5
Enter 5 integers:
53 75 23 64 43
1-Selection Sort
2-Bubble Sort
3-Insertion Sort
4-Radix Sort
5-Quick Sort
6-Heap Sort
7-Shell Sort
8-Merge Sort
9-Counting Sort
Enter choice: 3
Array sorted using Insertion Sort is:
23 43 53 64 75

S-ar putea să vă placă și