All kinds of sorts. insertion, merge, heap, counting, radix Flashcards
what are in-place alogrthim and mention exampels of it
algorithms that rearrange the elements within the array with at most a constant number stored outside the array at any time
selection sort
bubble sort
insertion sort
heap sort
For searching on sorted data by comparing keys, optimal solutions require
o(logn)
searching on unsorted data by comparing keys, optimal solutions require big o of
o(n)
what are stable algorthims
algorithms in which their elements appear in the output array in the same order as they do in the input array.
examples of algorithms that are both in place and stable algorithm
bubble sort and insertion sort
display how would insertion sort work on these numbers {6,3,9,8,24,8}
write psudocode for insertion sortand write its running time and the
whats insertion sort running time in the best case and worst case
when do both cases happen
best case O(n) liner function =an+b
Worst Case O(nˆ2)
best Case happens when it is already sorted
worst case when it’s in the opposite order
write running time for insertion sort pseudocode
represent insertion sort worst case mathmatically
T or F: In insertion sort, the average case is way better than the worst case.
F
almost same as bad
on average what is the condition of insertion sort problems
On average, half the elements in A [1 … j-1] are less than A[j], and half the elements are greater.
tj = ½ j
The resulting average-case running time for insertion sort turns out to be
a quadratic function of the input size.
merge sort to sort an array usually dose thee following
To sort an array A[p . . r]:
Divide
Divide the n-element sequence to be sorted into two subsequences of n/2 elements each
Conquer
Sort the subsequences recursively using merge sort.
When the size of the sequences is 1 there is nothing more to do.
Combine
Merge the two sorted subsequences.
merge sort
conquer merge sort
conquer
merge sort pausoxodw
whats the output of merging
One single sorted subarray A[p . . r]
1.
Idea for merging:
Two piles of sorted cards
Choose the smaller of the two top cards
Remove it and place it in the output pile
Repeat the process until one pile is empty
Take the remaining input pile and place it face-down onto the output pile
MERGE(A, 0, 3, 7)
A={1,6,43,8,0,4,6}
starting like this
what is merge pseudocode
its okay to look at code while figering that out
MERGE(A, p, q, r)
Compute n1 and n2
Copy the first n1 elements into
L[1 . . n1 + 1] and the next n2 elements into R[1 . . n2 + 1]
L[n1 + 1] ← ∞; R[n2 + 1] ← ∞
i ← 1; j ← 1
for k ← p to r
do if L[ i ] ≤ R[ j ]
then A[k] ← L[ i ]
i ←i + 1
else A[k] ← R[ j ]
j ← j + 1
Running Time of Merge
Initialization (copying into temporary arrays):
Θ(n1 + n2) = Θ(n)
Adding the elements to the final array:
n iterations, each taking constant time ⇒ Θ(n)
Total time for Merge:
Θ(n)
MERGE-SORT Running Time
Divide:
compute q as the average of p and r: D(n) = Θ(1)
Conquer:
recursively solve 2 sub-problems, each of size n/2 ⇒ 2T (n/2)
Combine:
MERGE on an n-element subarray takes Θ(n) time ⇒ C(n) = Θ(n)
The (binary) heap data structure is an …….. that we can view as a nearly complete binary tree.
array object
1.
Heap data structure is used for:
Heap sort
Priority queue
T or F:The binary heap tree is filled on all levels
F
A completely filled on all levels except possibly the lowest.
T or F: Each node of the tree corresponds to an element of the array
T
An array A that represents a heap is an object with two attributes:
A.length: number of elements in the array.
A.heap-size: represents how many elements in the heap are stored within array A.