CSIT Assignment 2
CSIT Assignment 2
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
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
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