0% found this document useful (0 votes)
58 views19 pages

Assignment 4.1: Name - Aryan Gupta Reg. No. 199301088 Sec - B

The document contains code for sorting and rearranging arrays using various algorithms like merge sort, quicksort, sorting a binary array, rearranging positive and negative numbers, and pushing all zeros to the end of an array. Test cases are provided to take input from the user, apply the sorting algorithm, and output the rearranged array.

Uploaded by

bruh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views19 pages

Assignment 4.1: Name - Aryan Gupta Reg. No. 199301088 Sec - B

The document contains code for sorting and rearranging arrays using various algorithms like merge sort, quicksort, sorting a binary array, rearranging positive and negative numbers, and pushing all zeros to the end of an array. Test cases are provided to take input from the user, apply the sorting algorithm, and output the rearranged array.

Uploaded by

bruh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Name – Aryan Gupta

Reg. No. 199301088


Sec – B

Assignment 4.1

Ans.1)

Code –

#include<stdio.h>
void mergesort(int x[],int n)
{
int i,j,k,size,l1,l2,u1,u2,aux[n];
size=1;
while (size < n)
{
l1 = 0;
k = 0;
while (l1+size < n)
{
l2=l1+size;
u1=l2-1;
u2=(l2+size-1)<n?(l2+size-1):n-1;
for (i = l1, j = l2; i <= u1 && j <= u2; k++)
{
if (x[i] <= x[j])
aux[k] = x[i++];
else
aux[k] = x[j++];
}
for (; i <= u1; k++)
aux[k] = x[i++];
for (; j <= u2; k++)
aux[k] = x[j++];

l1=u2+1;
}
for (i = l1; k < n; i++)
aux[k++] = x[i];
for (i = 0; i < n; i++)
x[i] = aux[i];
size *= 2;
}

void display(int x[],int n)


{
int i;
for(i=0;i<n;i++)
printf("%d ",x[i]);
}

void main()
{
int n;
printf("Enter array size: ");
scanf("%d",&n);
printf("Enter array elements:\n");
int a[n];
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}

printf("\n Merge Sort\n");


mergesort(a, n);
display(a, n);
}
Output –

Ans.2)

Code –

#include <stdio.h>

void printArray(int arr[], int n)


{
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

// Merges two subarrays of arr[].


// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;

/* create temp arrays */


int L[n1], R[n2];

/* Copy data to temp arrays L[] and R[] */


for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];

/* Merge the temp arrays back into arr[l..r]*/


i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray

// Note the order of appearance of elements should


// be maintained - we copy elements of left subarray
// first followed by that of right subarray
// copy negative elements of left subarray
while (i < n1 && L[i] < 0)
arr[k++] = L[i++];

// copy negative elements of right subarray


while (j < n2 && R[j] < 0)
arr[k++] = R[j++];

// copy positive elements of left subarray


while (i < n1)
arr[k++] = L[i++];

// copy positive elements of right subarray


while (j < n2)
arr[k++] = R[j++];
}

// Function to Rearrange positive and negative


// numbers in a array
void RearrangePosNeg(int arr[], int l, int r)
{
if (l < r) {
// Same as (l + r)/2, but avoids overflow for
// large l and h
int m = l + (r - l) / 2;
// Sort first and second halves
RearrangePosNeg(arr, l, m);
RearrangePosNeg(arr, m + 1, r);

merge(arr, l, m, r);
}
}

/* Driver program to test above functions */


int main()
{
int n;
printf("Enter array size: ");
scanf("%d",&n);
printf("Enter array elements:\n");
int a[n];
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}

printf("\nInputted array\n");
printArray(a, n);
printf("\nRearranged aray \n");
RearrangePosNeg(a, 0, n-1);
printArray(a, n);
return 0;
}

Output –
Assignment 4.2

Ans.1)

Code –

#include<stdio.h>

void swap(int *p,int *q)


{
int c;
c=*p;
*p=*q;
*q=c;
}

void quicksort(int x[],int lb,int ub)


{
int i,j,up,down,pivot;
down=lb;
up=ub;
pivot=x[lb];
if (lb>=ub)
return;
while(down<up)
{
while (x[down]<=pivot && down<=ub)
down++;
while (x[up]>pivot)
up--;
if (up>down)
swap(&x[down],&x[up]);
}
x[lb]=x[up];
x[up]=pivot;
quicksort(x,lb,up-1);
quicksort(x,up+1,ub);
}

void display(int x[],int n)


{
int i;

for(i=0;i<n;i++)
printf("%d ",x[i]);
}

void main()
{
int n;
printf("Enter array size: ");
scanf("%d",&n);
printf("Enter array elements:\n");
int a[n];
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}

quicksort(a,0,n-1);
printf("\nQuick Sort\n");
display(a,n);
}

Output –
Ans.2)

Code –

#include <stdio.h>

// Function to sort a binary array in linear time


int sort(int A[], int n)
{
// count number of 0's
int zeros = 0;
for (int i = 0; i < n; i++)
{
if (A[i] == 0) {
zeros++;
}
}

// put 0's at the beginning


int k = 0;
while (zeros--) {
A[k++] = 0;
}

// fill all remaining elements by 1


while (k < n) {
A[k++] = 1;
}
}

int main(void)
{
int n;
printf("Enter array size: ");
scanf("%d",&n);
printf("Enter array elements:\n");
int a[n];
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}

sort(a, n);

// print the rearranged array


for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}

return 0;
}

Output –
Ans.3)

Code –

#include <stdio.h>

void swap(int *p,int *q)


{
int c;
c=*p;
*p=*q;
*q=c;
}

void partition(int a[], int n)


{
int pIndex = 0;
for (int i = 0; i < n; i++)
{
if (a[i] < 0) // pivot is 0
{
swap(&a[i], &a[pIndex]);
pIndex++;
}
}
}

int main()
{
int n;
printf("Enter array size: ");
scanf("%d",&n);
printf("Enter array elements:\n");
int a[n];
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}

partition(a, n);

// print the rearranged array


for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}

return 0;
}

Output –

Ans.4)

Code –
#include <stdio.h>

// Function which pushes all zeros to end of an array.


void pushZerosToEnd(int arr[], int n)
{
int count = 0; // Count of non-zero elements

// Traverse the array. If element encountered is non-


// zero, then replace the element at index 'count'
// with this element
for (int i = 0; i < n; i++)
if (arr[i] != 0)
arr[count++] = arr[i]; // here count is
// incremented

// Now all non-zero elements have been shifted to


// front and 'count' is set as index of first 0.
// Make all elements 0 from count to end.
while (count < n)
arr[count++] = 0;
}

// Driver program to test above function


int main()
{
int n;
printf("Enter array size: ");
scanf("%d",&n);
printf("Enter array elements:\n");
int a[n];
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
pushZerosToEnd(a, n);
printf("\nRearranged array:\n");
for (int i = 0; i < n; i++)
printf("%d ",a[i]);
return 0;
}

Output –

You might also like