Describe Sorting:
What are the 2 Interfaces to implement Comparison functions?
Explain how Comparable works?
Explain how Comparator works?
Define Enumeration :
Define Exchange:
Define Selection:
Define Insertion:
Define Divide & Conquer:
Define Stable Sort :
Does not change the order of items in the input if they have the same sort key
Explain Bubble Sort:
Multiple passes over an array of items, swapping neighbouring items.
=> Only swaps neighbouring items that are out of place
=> First pass definitely get items in index 0, Second pass
second smallest items in index and so on
=> May get more than one value in the correct order
What is Complexity of Bubble Sort?
Outer Loop = n-1 times
Inner Loop = n-i times
thus total comparisons are : (n(n-2))/2 == (n-1)+(n-2)+…+(1)
==> (n^2-n)/2
// Each iteration executes a single comparison. Measuring complexity by counting No. of comparisons.
Is Bubble Sort Stable?
Yes, because bubble sort can only swap values strictly smaller than itself and will not swap values equivalent (THE SAME), thus does not change and they retain their order of elements.
NEVER SWAPS 2 RECORDS AROUND IF THEY HAVE SAME KEY VALUE
CODE for Bubble:
bubblesort(a,n){
for(i = 1 ; i<n; i++){
for(j = n-1; j>=i ; j–) { // Starts on last element and works
if(a[j] < a[j-1]) { // down
swap a[j] and a[j-1]
}
}
}
} // Basic version of Bubble Sort has no Optimisation
Define Insertion Sort:
Taking each element of the input and inserting it into its correct position relative to all the elements that have been inserted so far
- Sorted part is usually the first element in the array and the rest is unsorted
- Takes the first element of the unsorted part & inserts it into its correct position in the sorted part,
(Growing sorted part & shrinking the unsorted by one cell)
Complexity of Insertion Sort?
Is insertion Sort Stable?
IS STABLE, BECAUSE:
- 1st Occurrence of 2 copies of the same value will be taken
before 2nd.
- Must Be strictly greater than to cause a swap
- We will not insert later copy of a value before an earlier
copy that has already been inserted
Define Selection Sort:
Works by Selecting the smallest remaining element of the input array and appending it at the end of all the elements that have been inserted so far.
SPLITS ARRAY INTO 2 PARTS SORTED & UNSORTED
Describe Selection Sort:
First Pass: smallest item from unsorted part and swap with first element and then next smallest swap with the second element repeat till last smallest element is found
Look through unsorted part of the Array & find what’s the next VAL we have to put in
Selection Sort Code
selectionSort(a (list) , n (size)){
for(i =0 ; i < n-1 ; i++){
k = i
for(j= i+1 ; j<n ; j++){ // Linear search to find
if(a[j]<a[k]){ // Smallest value. if smaller
k = j // swap & index j is smallest
}
}
swap a[i] and a[k] // swaps elements
around
}
}
Selection Sort Complexity:
Selection Sort Stability:
Shuffling unsorted parts of the Array. Therefore, thus changes the position of element. Thus, Changing order, so NOT STABLE.
e.g:
list: 2_1,2_2,1
swaps 2_1 with 1 thus swapping index 0 and 2
list: 1,2_2,2_1
No more swaps, But the order of 2s don’t change
Describe Heap Sort: