Guest User

Untitled

a guest
Aug 20th, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. Insert element into sorted list
  2. Given a sorted list with an unsorted number V in the right-most cell, can you write some simple code to insert V into the array so it remains sorted?
  3.  
  4. Print the array every time a value is shifted in the array until the array is fully sorted. The goal of this challenge is to follow the correct order of insertion sort.
  5.  
  6. Guideline: You can copy the value of V to a variable, and consider its cell “empty”. Since this leaves an extra cell empty on the right, you can shift everything over until V can be inserted. This will create a duplicate of each value, but when you reach the right spot, you can replace a value with V.
  7.  
  8. Input Format
  9. There will be two lines of input:
  10.  
  11. s - the size of the array
  12. ar - the sorted array of integers
  13. Output Format
  14. On each line, output the entire array every time an item is shifted in it.
  15.  
  16. Constraints
  17. 1<=s<=1000
  18. -10000<=x<= 10000, x ∈ ar
  19.  
  20. Sample Input
  21.  
  22. 5
  23. 2 4 6 8 3
  24. Sample Output
  25.  
  26. 2 4 6 8 8
  27. 2 4 6 6 8
  28. 2 4 4 6 8
  29. 2 3 4 6 8
  30. Explanation
  31.  
  32. 3 is removed from the end of the array.
  33. In the 1st line 8 > 3, 8 is shifted one cell right.
  34. In the 2nd line 6 > 3, 6 is shifted one cell right.
  35. In the 3rd line 4 > 3, 4 is shifted one cell right.
  36. In the 4th line 2 < 3, 3 is placed at position 2.
Advertisement
Add Comment
Please, Sign In to add comment