C Programming: Sorting Algorithms Tasks
C Programming: Sorting Algorithms Tasks
8
05
05
05
0
01
01
01
01
15
15
15
15
Name: Gopika V Scan to verify results
24
24
24
24
Email: 241501058@[Link]
Roll no: 241501058
Phone: 6380725844
Branch: REC
Department: I AIML AD
Batch: 2028
Degree: B.E - AI & ML
8
58
58
8
05
05
10
0
NeoColab_REC_CS23231_DATA STRUCTURES
01
01
01
0
15
15
15
15
24
24
24
24
REC_DS using C_Week 6_COD_Question 1
Attempt : 1
Total Mark : 10
Marks Obtained : 10
Section 1 : Coding
1. Problem Statement
58
58
8
05
05
0
John and Mary are collaborating on a project that involves data analysis.
01
01
01
01
15
15
15
They each have a set of age data, one sorted in ascending order and the 15
24
24
24
24
other in descending order. However, their analysis requires the data to be in
ascending order.
Write a program to help them merge the two sets of age data into a single
sorted array in ascending order using merge sort.
Input Format
The first line of input consists of an integer N, representing the number of age
values in each dataset.
8
8
05
05
05
05
01
01
01
01
15
15
15
24
24
24
58
8
05
05
05
The third line consists of N space-separated integers, representing the ages of
0
01
01
01
01
participants in Mary's dataset (in descending order).
15
15
15
15
24
24
24
24
Output Format
The output prints a single line containing space-separated integers, which
represents the merged dataset of ages sorted in ascending order.
58
58
8
05
05
10
0
Input: 5
01
01
01
0
13579
15
15
15
15
24
24
24
24
10 8 6 4 2
Output: 1 2 3 4 5 6 7 8 9 10
Answer
#include <stdio.h>
// You are using GCC
void merge(int arr[], int left[], int right[], int left_size, int right_size) {
int i=0,j=0,k=0;
while(i<left_size && j<right_size)
if(left[i]<right[j])
58
58
8
05
05
{
0
0
01
01
01
01
arr[k++]=left[i++];
15
15
15
}else{ 15
24
24
24
24
arr[k++]=right[j++];
}
while(i<left_size){
arr[k++]=left[i++];
}
while(j<right_size){
arr[k++]=right[j++];
}
}
8
8
05
05
05
05
01
01
01
01
15
15
15
24
24
24
24
if(size<2) return;
58
8
05
05
05
int mid=size/2;
0
01
01
01
01
int left[mid];
15
15
15
15
int right[size-mid];
24
24
24
24
for(int i=0;i<mid;i++){
left[i]=arr[i];
}
for(int i=mid;i<size;i++){
right[i-mid]=arr[i];
}
mergeSort(left,mid);
mergeSort(right,size-mid);
merge(arr,left,right,mid,size-mid);
}
8
58
58
8
05
05
10
0
01
01
01
int main() {
0
15
15
15
15
int n, m;
24
24
24
24
scanf("%d", &n);
int arr1[n], arr2[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr1[i]);
}
for (int i = 0; i < n; i++) {
scanf("%d", &arr2[i]);
}
int merged[n + n];
mergeSort(arr1, n);
58
58
8
mergeSort(arr2, n);
05
05
0
0
01
01
01
01
merge(merged, arr1, arr2, n, n);
15
15
15
24
24
24
printf("%d ", merged[i]);
}
return 0;
}
8
05
05
05
05
01
01
01
01
15
15
15
15
24
24
24
24
58 Rajalakshmi Engineering College
8
05
05
05
0
01
01
01
01
15
15
15
15
Name: Gopika V Scan to verify results
24
24
24
24
Email: 241501058@[Link]
Roll no: 241501058
Phone: 6380725844
Branch: REC
Department: I AIML AD
Batch: 2028
Degree: B.E - AI & ML
8
58
58
8
05
05
10
0
NeoColab_REC_CS23231_DATA STRUCTURES
01
01
01
0
15
15
15
15
24
24
24
24
REC_DS using C_Week 6_COD_Question 2
Attempt : 1
Total Mark : 10
Marks Obtained : 10
Section 1 : Coding
1. Problem Statement
58
58
8
05
05
0
01
01
01
15
15
15
order. She asked the students to arrange the elements using insertion sort, 15
24
24
24
24
which involves taking each element and placing it in its appropriate
position within the sorted portion of the array.
Input Format
The first line of input consists of the value of n, representing the number of array
elements.
8
05
05
05
01
01
01
01
Output Format
15
15
15
15
24
24
24
24
The output prints the sorted array, separated by a space.
58
8
05
05
05
0
01
01
01
01
15
15
15
15
24
24
24
24
Refer to the sample output for formatting specifications.
Sample Test Case
Input: 5
67 28 92 37 59
Output: 28 37 59 67 92
Answer
#include <stdio.h>
8
58
58
8
05
05
10
0
01
01
01
// You are using GCC
0
15
15
15
15
24
24
24
24
void insertionSort(int arr[], int n) {
//Type your code here
for(int i=1;i<n;i++){
int key=arr[i];
int j=i-1;
58
8
05
05
arr[j+1]=key;
0
0
01
01
01
01
}
15
15
15
} 15
24
24
24
24
8
05
05
05
05
int main() {
01
01
01
01
15
15
15
15
int n;
24
24
24
24
scanf("%d", &n);
58
8
05
05
05
int arr[n];
0
01
01
01
01
for (int i = 0; i < n; i++) {
15
15
15
15
scanf("%d", &arr[i]);
24
24
24
24
}
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
58
58
8
05
05
10
0
01
01
01
0
15
15
15
15
24
24
24
24
58
58
8
05
05
0
0
01
01
01
01
15
15
15
15
24
24
24
24
8
8
05
05
05
05
01
01
01
01
15
15
15
15
24
24
24
24
58 Rajalakshmi Engineering College
8
05
05
05
0
01
01
01
01
15
15
15
15
Name: Gopika V Scan to verify results
24
24
24
24
Email: 241501058@[Link]
Roll no: 241501058
Phone: 6380725844
Branch: REC
Department: I AIML AD
Batch: 2028
Degree: B.E - AI & ML
8
58
58
8
05
05
10
0
NeoColab_REC_CS23231_DATA STRUCTURES
01
01
01
0
15
15
15
15
24
24
24
24
REC_DS using C_Week 6_COD_Question 3
Attempt : 1
Total Mark : 10
Marks Obtained : 10
Section 1 : Coding
1. Problem Statement
58
58
8
05
05
0
01
01
01
15
15
15
24
24
24
sorting service that helps users highlight the most critical elements of their
text.
Input Format
8
The first line of the input consists of a positive integer value N, representing the
05
05
05
05
01
01
01
01
15
15
15
24
24
24
24
58
8
05
05
05
The second line of input consists of N space-separated lowercase alphabetical
0
01
01
01
01
characters.
15
15
15
15
24
24
24
24
Output Format
The output displays the set of alphabetical characters, sorted in descending
order.
58
58
8
05
05
10
0
Input: 5
01
01
01
0
adgjk
15
15
15
15
24
24
24
24
Output: k j g d a
Answer
#include <stdio.h>
#include <string.h>
void swap(char* a, char* b){
char temp = *a;
*a = *b;
*b = temp;
}
58
58
8
05
05
int partition(char arr[], int low, int high)
0
0
01
01
01
01
{
15
15
15
24
24
24
int i = low - 1;
for (int j = low; j < high; j++)
{
if (arr[j] > pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
8
8
05
05
05
05
return i + 1;
01
01
01
01
}
15
15
15
15
24
24
24
24
void quicksort(char arr[], int low, int high)
58
8
05
05
05
{
0
01
01
01
01
if (low < high)
15
15
15
15
{
24
24
24
24
int pi = partition(arr, low, high);
quicksort(arr, low, pi - 1);
quicksort(arr, pi + 1, high);
}
}
int main() {
int n;
scanf("%d", &n);
8
58
58
8
char characters[n];
05
05
10
0
01
01
01
0
15
15
15
15
for (int i = 0; i < n; i++) {
24
24
24
24
char input;
scanf(" %c", &input);
characters[i] = input;
}
quicksort(characters, 0, n - 1);
58
8
05
05
0
0
01
01
01
01
return 0;
15
15
15
}
15
24
24
24
24
8
05
05
05
05
01
01
01
01
15
15
15
15
24
24
24
24
58 Rajalakshmi Engineering College
8
05
05
05
0
01
01
01
01
15
15
15
15
Name: Gopika V Scan to verify results
24
24
24
24
Email: 241501058@[Link]
Roll no: 241501058
Phone: 6380725844
Branch: REC
Department: I AIML AD
Batch: 2028
Degree: B.E - AI & ML
8
58
58
8
05
05
10
0
NeoColab_REC_CS23231_DATA STRUCTURES
01
01
01
0
15
15
15
15
24
24
24
24
REC_DS using C_Week 6_COD_Question 4
Attempt : 1
Total Mark : 10
Marks Obtained : 10
Section 1 : Coding
1. Problem Statement
58
58
8
05
05
0
01
01
01
15
15
15
integers and wants to identify the nth largest number in the list after 15
24
24
24
24
sorting the array using QuickSort.
Input Format
The first line of input consists of an integer n, representing the size of the array.
05
05
05
01
01
01
01
15
15
15
15
The third line consists of an integer k, representing the position of the largest
24
24
24
24
number you need to print after sorting the array.
58
8
05
05
05
0
01
01
01
01
Output Format
15
15
15
15
24
24
24
24
The output prints the k-th largest number in the sorted array (sorted in ascending
order).
58
58
8
05
05
10
0
3
01
01
01
0
15
15
15
15
Output: 0
24
24
24
24
Answer
#include <stdio.h>
#include <stdlib.h>
void swap(int* a, int* b){
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[], int low, int high)
58
58
8
05
05
{
0
0
01
01
01
01
int pivot = arr[high];
15
15
15
int i = low - 1; 15
24
24
24
24
for (int j = low; j < high; j++)
{
if (arr[j] < pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return i + 1;
8
8
05
05
05
05
}
01
01
01
01
15
15
15
24
24
24
24
{
58
8
05
05
05
if (low < high)
0
01
01
01
01
{
15
15
15
15
int pi = partition(arr, low, high);
24
24
24
24
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void findNthLargest(int* nums, int n, int k)
{
quickSort(nums, 0, n - 1);
printf("%d", nums[n - k]);
}
8
58
58
8
int main() {
05
05
10
0
01
01
01
int n, k;
0
15
15
15
15
scanf("%d", &n);
24
24
24
24
int* nums = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
scanf("%d", &nums[i]);
}
scanf("%d", &k);
findNthLargest(nums, n, k);
free(nums);
return 0;
}
58
58
8
Status : Correct Marks : 10/10
05
05
0
0
01
01
01
01
15
15
15
15
24
24
24
24
8
8
05
05
05
05
01
01
01
01
15
15
15
15
24
24
24
24
58 Rajalakshmi Engineering College
8
05
05
05
0
01
01
01
01
15
15
15
15
Name: Gopika V Scan to verify results
24
24
24
24
Email: 241501058@[Link]
Roll no: 241501058
Phone: 6380725844
Branch: REC
Department: I AIML AD
Batch: 2028
Degree: B.E - AI & ML
8
58
58
8
05
05
10
0
NeoColab_REC_CS23231_DATA STRUCTURES
01
01
01
0
15
15
15
15
24
24
24
24
REC_DS using C_Week 6_COD_Question 5
Attempt : 1
Total Mark : 10
Marks Obtained : 10
Section 1 : Coding
1. Problem Statement
58
58
8
05
05
0
01
01
01
15
15
15
24
24
24
your help.
Write a program to help Jose sort the array using the merge sort algorithm.
Input Format
The first line of input consists of an integer N, representing the number of
fractions to be sorted.
05
05
05
01
01
01
01
Output Format
15
15
15
15
24
24
24
24
The output prints N double-point numbers, sorted in increasing order, and
58
8
05
05
05
rounded to three decimal places.
0
01
01
01
01
15
15
15
15
24
24
24
24
Refer to the sample output for formatting specifications.
Sample Test Case
Input: 4
0.123 0.543 0.321 0.789
Output: 0.123 0.321 0.543 0.789
Answer
8
58
58
8
05
05
10
0
#include <stdio.h>
01
01
01
0
15
15
15
15
#include <stdlib.h>
24
24
24
24
int compare(double a, double b){
if (a < b) return -1;
else if (a > b) return 1;
else return 0;
}
void merge(double arr[], int l, int m, int r)
{
int n1 = m - l + 1;
int n2 = r - m;
double L[n1], R[n2];
58
58
8
05
05
for (int i = 0; i < n1; i++)
0
0
01
01
01
01
L[i] = arr[l + i];
15
15
15
24
24
24
R[j] = arr[m + 1 + j];
int i = 0, j = 0, k = l;
while (i < n1 && j < n2)
{
if (compare(L[i], R[j]) <= 0)
{
arr[k] = L[i];
i++;
} else
{
8
8
05
05
05
05
arr[k] = R[j];
01
01
01
01
j++;
15
15
15
15
24
24
24
24
}
58
8
05
05
05
k++;
0
01
01
01
01
}
15
15
15
15
while (i < n1)
24
24
24
24
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
8
58
58
8
05
05
}
10
0
01
01
01
0
}
15
15
15
15
void mergeSort(double arr[], int l, int r)
24
24
24
24
{
if (l < r)
{
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
58
58
8
int main() {
05
05
0
0
01
01
01
01
int n;
15
15
15
scanf("%d", &n);
15
24
24
24
24
double fractions[n];
for (int i = 0; i < n; i++) {
scanf("%lf", &fractions[i]);
}
mergeSort(fractions, 0, n - 1);
for (int i = 0; i < n; i++) {
printf("%.3f ", fractions[i]);
}
return 0;
}
8
8
05
05
05
05
01
01
01
01
15
15
15
24
24
24
24