0% found this document useful (0 votes)
16 views3 pages

Sorting Solution To Assignment 02 Written DECODE DSA With C 2.0652649a67774af00183e8ef1

The document discusses selection and insertion sorting algorithms, providing solutions to related questions. It includes code examples for insertion sort and bubble sort, demonstrating how to sort arrays of integers and strings. Key points include the mechanics of selection sort and the worst-case scenario for insertion sort.

Uploaded by

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

Sorting Solution To Assignment 02 Written DECODE DSA With C 2.0652649a67774af00183e8ef1

The document discusses selection and insertion sorting algorithms, providing solutions to related questions. It includes code examples for insertion sort and bubble sort, demonstrating how to sort arrays of integers and strings. Key points include the mechanics of selection sort and the worst-case scenario for insertion sort.

Uploaded by

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

Assignment Solutions | Selection and insertion sorting | Week 9

1. Which of the following is true about selection sort:


a) In each iteration we find the minimum element in the unsorted part of the array.
b) In each iteration we find the index of the minimum element in the unsorted part of the array.
c) We swap the index of the minimum element with the first element of the array.
d) It takes O(n^2) swaps.

Solution :
b)In each iteration we find the index of the minimum element in the unsorted
part of the array.

Which of the following examples represent the worst case input for an insertion sort?
a) array in sorted order
b) large array
c) normal unsorted array
d) array sorted in reverse order

Solution :
d) array sorted in reverse order.

How many passes would be required during insertion sort to sort an array of 5 elements?
a) 1
b) Depends on order of elements
c) 4
d) 5

Solution :
c) 4

Given an array of digits (values are from 0 to 9), the task is to find the minimum possible sum of
two numbers formed from digits of the array. Please note that all digits of the given array must be
used to form the two numbers.

Solution :

1
#include <iostream>
using namespace std;

void insertionSort(int arr[], int n){


int i, key, j;
for (i = 1; i < n; i++){
key = arr[i];
j = i - 1;

while (j >= 0 && arr[j] > key){


arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

int main() {
int arr[5]={7,2,32,5,20};
int N=5;
insertionSort(arr,N);

int a = 0, b = 0;

Given an array of strings arr[] with all strings in lowercase. Sort given strings using Bubble Sort
and display the sorted array.

Solution :

2
#include<iostream>
#include<cstring>
using namespace std;
int main() {
char arr[][20] = {"physicswallah", "quiz", "practice", "pwskills","coding" };
int n = sizeof(arr) / sizeof(arr[0]);
char temp[20];
for (int i = 0; i < n ; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (strcmp(arr[j], arr[j + 1]) > 0) {
strcpy(temp, arr[j]);
strcpy(arr[j], arr[j + 1]);
strcpy(arr[j + 1], temp);
}
}
}
for (int i = 0; i < n; i++) {
cout<<arr[i]<<endl;
}
return 0;
}

You might also like