Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. 1. Write pseudocode for bubble sort.
  2.  
  3. FUNCTION bubbleSort(data)
  4. SET swapped to false
  5.  
  6. FOR i = FIRST index of data to LAST index of data - 1
  7. IF data[i] > data [i + 1]
  8. SET tmp to data[i]
  9. set data[i] to data [i+1]
  10. set data [i+1] to tmp
  11. set swapped to true
  12. END IF
  13. END FOR
  14. UNTIL swapped is false
  15. RETURN data
  16. END FUNCTION
  17.  
  18.  
  19. 2.Write pseudocode for quicksort.
  20.  
  21. FUNCTION quickSort(array)
  22.  
  23. IF (array length < 2)
  24. RETURN array
  25.  
  26. SET pivot to array[0]
  27.  
  28. FOR FIRST index of array to length or array i++
  29. IF array[i] is less than pivot
  30. lesser push array[i]
  31. ELSE
  32. greater push array [i]
  33. RETURN quickSort(lesser).concat(pivot,quickSort(greater));
  34. END FUNCTION
  35.  
  36.  
  37. 3.We talked about time complexity in a previous checkpoint, and how to get an idea
  38. of the efficiency of an algorithm. After looking at the pseudocode for the above
  39. sorting methods, identify why merge sort and quick sort are much more efficient
  40. than the others. Walking through each algorithm with a few sample collections may
  41. help.
  42.  
  43. Insertion sort must iterate through the entire data set multiple times until the
  44. set is sorted, this makes it a costly process particularly for large data sets.
  45. Selection sort uses a nested for loop, which gives it a time complexity of O(n^2).
  46. With bubble sort, you run the risk of having to make several passes through a large
  47. data set, which also gives the algorithm a time complexity of O(n^2).
  48.  
  49. Merge sort and quick sort both have time complexities of O(n log (n)). They accomplish
  50. this by only iterating through the entire array once, and then splitting work into
  51. smaller more manageable data sets.
  52.  
  53. 4.All of the sorts addressed in this checkpoint are known as comparison sorts.
  54. Research bucket sort and explain how it works. What is the ideal input for bucket
  55. sort?
  56.  
  57. Bucket sort separates values into buckets, each bucket is then individually sorted
  58. and finally, the sorted buckets are placed back into an array. The ideal input for
  59. a bucket sort is an array.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement