Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Some Data Structure problems with test cases, Exams of Data Structures and Algorithms

These problems related to Sorting

Typology: Exams

2017/2018

Uploaded on 09/02/2018

venubabu_r
venubabu_r 🇮🇳

1 document

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1. Given two sorted arrays X[ ] and Y[ ] of size m and n each, merge elements of X[ ]
with elements of array Y[ ] by maintaining the sorted order. i.e. fill X[ ] with first m
smallest elements and fill Y[ ] with remaining elements.
Input:
First number denotes number of elements in first array m and second number
denotes number of elements in second array n. Next m+n numbers are elements of
two arrays individually sorted.
Output:
Printing two arrays in sorted order attaining number of elements in the arrays.
Example:
Input:
4
3
2 4 6 8
1 3 5
Output:
1 2 3 4
5 6 8
Test Cases:
Case2:
Input:
4
5
1 3 5 7
2 4 6 8 10
pf3
pf4
pf5

Partial preview of the text

Download Some Data Structure problems with test cases and more Exams Data Structures and Algorithms in PDF only on Docsity!

  1. (^) Given two sorted arrays X[ ] and Y[ ] of size m and n each, merge elements of X[ ] with elements of array Y[ ] by maintaining the sorted order. i.e. fill X[ ] with first m smallest elements and fill Y[ ] with remaining elements. Input: First number denotes number of elements in first array m and second number denotes number of elements in second array n. Next m+n numbers are elements of two arrays individually sorted. Output: Printing two arrays in sorted order attaining number of elements in the arrays. Example: Input: 4 3 2 4 6 8 1 3 5 Output: 1 2 3 4 5 6 8 Test Cases: Case2: Input: 4 5 1 3 5 7

Output: 1 2 3 4 5 6 7 8 9 10 Case 3: Input: 6 5 1 2 5 7 8 10 3 11 13 15 16 Output: 1 2 3 5 7 8 10 11 13 15 16 Case 4: Input: 3 2 11 12 13 9 10 Output: 9 10 11 12 13

  1. Given two sorted arrays X[ ] and Y[ ] of size m and n each where m >= n and X[ ] has exactly n vacant cells(vacant cell is denoted by 0), merge elements of Y[ ] in their correct position in array X[ ] i.e. merge (X, Y) by keeping the sorted order. Input:

Case 3:

Input: 8 5 1 0 4 0 5 0 0 0 2 6 7 8 9 Output: 1 2 4 5 6 7 8 9 Case 4: Input: 11 6 6 0 0 8 10 15 0 18 0 0 0 2 3 4 7 8 9 2 3 4 6 7 8 8 9 10 15 18 Case 5: Input: 11 6 0 11 0 12 0 13 0 14 0 15 0 11 12 13 14 15 16

  1. The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array. 1) The subarray which is already sorted. 2) Remaining subarray which is unsorted. In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray. Now, Implement the selection sort Iterative way
  2. Implement the above sorting in recursive way.
  3. Given two sorted arrays X[ ] and Y[ ] of size m and n each where m >= n and X[ ] has exactly n vacant cells(vacant cell is denoted by 0), merge elements of Y[ ] in their correct position in array X[ ] i.e. merge (X, Y) by keeping the sorted order. Input: First number denotes number of elements in first array m and second number denotes number of elements in second array n. Next m+n numbers are elements of two arrays. X[ ] = { 0, 2, 0, 3, 0, 5, 6, 0, 0} Y[ ] = { 1, 8, 9, 10, 15 } The vacant cells in X[ ] is represented by 0

Output: Merging the second array elements in their correct position in first array. X[ ] = { 1, 2, 3, 5, 6, 8, 9, 10, 15 } The above can be represented

Output: -5 -4 9

Case 4: Input: 8 -2 -3 -4 -5 1 2 3 4 Output: -5 -4 4

Case 5: Input: 7 -1 -2 -3 -4 -5 -6 - Output: -1 -2 -

  1. How do you implement if you want to find minimum product in the above question
  2. Given an array of integers, move all 0’s present must move to end without changing relative order of non-zeros.

Input: First line integer represents number of array elements and Next line contains n integers Output: print the non-zeros in the relative order flowed by all zeros present in the array Example:- Input: 7 4 0 2 0 3 0 9 Output: