Data Structure: Two Mark Questions
Data Structure: Two Mark Questions
Merge sort is an example of external sorting. This sorts the sub arrays and the sorted sub
arrays are stored in an auxiliary table. Then the sorted sub arrays are brought in to main
memory for merging and sorting.
10) What is bubble sort?
Bubble sort arranges the array of integers as x[i]<=x[j], 0<=I<j
This was given the name as each element bubbles to the proper position.
11) What is quick sort or partition exchange sort?
Quick sort moves the data item into the correct direction, with the following rules:
i) Select a pivot element, from the array x.
ii) Initialize the down and up as the lower bound and upper bound of array.
iii) Increment down pointer, until x[down]>a
iv) Decrement up pointer, until x[up]<=a v) If up>down, interchange x[down] with x[up]
vi) If up<=down, interchange x[up] with pivot.
12) What is Pivot?
The element around which a file is partitioned is called a pivot.
13) What are the different methods for choosing a pivot?
i) The median of the first, last and middle elements of the sub file to be sorted can be chosen as
pivot.
ii) The mean sort uses the mean of the sub file as pivot for sorting.
iii) Bsort uses the middle element of subfile as pivot.
14) What is selection sort?
Selection sort is one in which successive elements are sorted in order and placed into their
proper sorted position.
15) What is straight selection sort or push down sort?
Straight selection sort is an in-place sort, from which the largest element should be chosen and
interchanged with the element at end.
16) What is binary tree sort?
In binary tree sort, a binary tree of descending priority queue is created first and inorder
traversing of the tree gives a sorted list.
17) What is Heap sort?
Heap sort is also an in-place sort, which creates a binary search tree in which elements are to
be sorted and stored.
18) What is descending heap/Max heap/Descending partially ordered queue?
Descending heap of size n is an almost complete binary tree of nodes such that the content of
each node is less than or equal to the content of its father.
Bubble sort
From Wikipedia, the free encyclopedia
Jump to: navigation, search
Bubble sort, sometimes incorrectly referred to as sinking sort, is a simple sorting
algorithm that works by repeatedly stepping through the list to be sorted,
comparing each pair of adjacent items and swapping them if they are in the wrong
order. The pass through the list is repeated until no swaps are needed, which
indicates that the list is sorted. The algorithm gets its name from the way smaller
elements "bubble" to the top of the list. Because it only uses comparisons to
operate on elements, it is a comparison sort. Although the algorithm is simple, most
other algorithms are more efficient for sorting large lists.
Bubble sort
Class
Data structure
Worst case performance
Best case performance
Sorting algorithm
Array
auxiliary
Bubble sort, sometimes incorrectly referred to as sinking sort, is a simple sorting algorithm that
works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items
and swapping them if they are in the wrong order. The pass through the list is repeated until no
swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the
way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate
on elements, it is a comparison sort. Although the algorithm is simple, most other algorithms are
more efficient for sorting large lists.
Performance
Bubble sort has worst-case and average complexity both (n2), where n is the number of items
being sorted. There exist many sorting algorithms with substantially better worst-case or average
complexity of O(n log n). Even other (n2) sorting algorithms, such as insertion sort, tend to
have better performance than bubble sort. Therefore, bubble sort is not a practical sorting
algorithm when n is large.
The only significant advantage that bubble sort has over most other implementations, even
quicksort, but not insertion sort, is that the ability to detect that the list is sorted is efficiently
built into the algorithm. Performance of bubble sort over an already-sorted list (best-case) is
O(n). By contrast, most other algorithms, even those with better average-case complexity,
perform their entire sorting process on the set and thus are more complex. However, not only
does insertion sort have this mechanism too, but it also performs better on a list that is
substantially sorted (having a small number of inversions).
Rabbits and turtles
The positions of the elements in bubble sort will play a large part in determining its performance.
Large elements at the beginning of the list do not pose a problem, as they are quickly swapped.
Small elements towards the end, however, move to the beginning extremely slowly. This has led
to these types of elements being named rabbits and turtles, respectively.
Various efforts have been made to eliminate turtles to improve upon the speed of bubble sort.
Cocktail sort is a bi-directional bubble sort that goes from beginning to end, and then reverses
itself, going end to beginning. It can move turtles fairly well, but it retains O(n2) worst-case
complexity. Comb sort compares elements separated by large gaps, and can move turtles
extremely quickly before proceeding to smaller and smaller gaps to smooth out the list. Its
average speed is comparable to faster algorithms like quicksort.
Step-by-step example
Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest
number using bubble sort. In each step, elements written in bold are being compared. Three
passes will be required.
First Pass:
( 5 1 4 2 8 ) ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5
> 1.
( 1 5 4 2 8 ) ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm
does not swap them.
Second Pass:
(14258) (14258)
( 1 4 2 5 8 ) ( 1 2 4 5 8 ), Swap since 4 > 2
(12458) (12458)
(12458) (12458)
Now, the array is already sorted, but our algorithm does not know if it is completed. The
algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
(12458) (12458)
(12458) (12458)
(12458) (12458)
(12458) (12458)
Implementation
Pseudocode implementation
The bubble sort algorithm can be easily optimized by observing that the n-th pass finds the n-th
largest element and puts it into its final place. So, the inner loop can avoid looking at the last n-1
items when running for the n-th time:
procedure bubbleSort( A : list of sortable items )
n = length(A)
repeat
swapped = false
for i = 1 to n-1 inclusive do
if A[i-1] > A[i] then
swap(A[i-1], A[i])
swapped = true
end if
end for
n = n - 1
until not swapped
end procedure
More generally, it can happen that more than one element is placed in their final position on a
single pass. In particular, after every pass, all elements after the last swap are sorted, and do not
need to be checked again. This allows us to skip over a lot of the elements, resulting in about a
worst case 50% improvement in comparison count (though no improvement in swap counts), and
adds very little complexity because the new code subsumes the "swapped" variable:
To accomplish this in pseudocode we write the following:
procedure bubbleSort( A : list of sortable items )
n = length(A)
repeat
newn = 0
for i = 1 to n-1 inclusive do
if A[i-1] > A[i] then
swap(A[i-1], A[i])
newn = i
end if
end for
n = newn
until n = 0
end procedure
Alternate modifications, such as the cocktail shaker sort attempt to improve on the bubble sort
performance while keeping the same idea of repeatedly comparing and swapping adjacent items.
In practice
A bubble sort, a sorting algorithm that continuously steps through a list, swapping
items until they appear in the correct order. Note that the largest end gets sorted
first, with smaller elements taking longer to move to their correct positions.
Although bubble sort is one of the simplest sorting algorithms to understand and implement, its
O(n2) complexity means that its efficiency decreases dramatically on lists of more than a small
number of elements. Even among simple O(n2) sorting algorithms, algorithms like insertion sort
are usually considerably more efficient.
Due to its simplicity, bubble sort is often used to introduce the concept of an algorithm, or a
sorting algorithm, to introductory computer science students. However, some researchers such as
Owen Astrachan have gone to great lengths to disparage bubble sort and its continued popularity
in computer science education, recommending that it no longer even be taught.[1]
The Jargon file, which famously calls bogosort "the archetypical perversely awful algorithm",
also calls bubble sort "the generic bad algorithm".[2] Donald Knuth, in his famous book The Art
of Computer Programming, concluded that "the bubble sort seems to have nothing to recommend
it, except a catchy name and the fact that it leads to some interesting theoretical problems", some
of which he then discusses.[3]
Bubble sort is asymptotically equivalent in running time to insertion sort in the worst case, but
the two algorithms differ greatly in the number of swaps necessary. Experimental results such as
those of Astrachan have also shown that insertion sort performs considerably better even on
random lists. For these reasons many modern algorithm textbooks avoid using the bubble sort
algorithm in favor of insertion sort.
Bubble sort also interacts poorly with modern CPU hardware. It requires at least twice as many
writes as insertion sort, twice as many cache misses, and asymptotically more branch
mispredictions. Experiments by Astrachan sorting strings in Java show bubble sort to be roughly
5 times slower than insertion sort and 40% slower than selection sort.[1]
In computer graphics it is popular for its capability to detect a very small error (like swap of just
two elements) in almost-sorted arrays and fix it with just linear complexity (2n). For example, it
is used in a polygon filling algorithm, where bounding lines are sorted by their x coordinate at a
specific scan line (a line parallel to x axis) and with incrementing y their order changes (two
elements are swapped) only at intersections of two lines.
Selection sort
In computer science, a selection sort is a sorting algorithm, specifically an in-place
comparison sort. It has O(n2) time complexity, making it inefficient on large lists,
and generally performs worse than the similar insertion sort. Selection sort is noted
for its simplicity, and also has performance advantages over more complicated
algorithms in certain situations, particularly where auxiliary memory is limited.
Algorithm
The algorithm works as follows:
1. Find the minimum value in the list
2. Swap it with the value in the first position
3. Repeat the steps above for the remainder of the list (starting at the second
position and advancing each time)
Effectively, the list is divided into two parts: the sublist of items already sorted, which is built up
from left to right and is found at the beginning, and the sublist of items remaining to be sorted,
occupying the remainder of the array.
Here is an example of this sort algorithm sorting five elements:
64 25 12 22 11
11 25 12 22 64
11 12 25 22 64
11 12 22 25 64
11 12 22 25 64
(nothing appears changed on this last line because the last 2 numbers were already in order)
Selection sort can also be used on list structures that make add and remove efficient, such as a
linked list. In this case it is more common to remove the minimum element from the remainder
of the list, and then insert it at the end of the values sorted so far. For example:
64 25 12 22 11
11 64 25 12 22
11 12 64 25 22
11 12 22 64 25
11 12 22 25 64
/* a[0] to a[n-1] is the array to sort */
int i,j;
int iMin;
/* advance the position through the entire array */
/*
(could do j < n-1 because single element is also min element) */
for (j = 0; j < n-1; j++) {
/* find the min element in the unsorted a[j .. n-1] */
/* assume the min is the first element */
iMin = j;
/* test against elements after j to find the smallest */
for ( i = j+1; i < n; i++) {
/* if this element is less, then it is the new minimum */
if (a[i] < a[iMin]) {
/* found new minimum; remember its index */
iMin = i;
}
}
/* iMin is the index of the minimum element. Swap it with the current
position */
if ( iMin != j ) {
swap(a[j], a[iMin]);
}
}
Mathematical definition
Let
3.
is a permutation of
for all
such that
where:
,
and
4.
5.
, and
without one instance of the smallest element of
Analysis
Selection sort is not difficult to analyze compared to other sorting algorithms since none of the
loops depend on the data in the array. Selecting the lowest element requires scanning all n
elements (this takes n 1 comparisons) and then swapping it into the first position. Finding the
next lowest element requires scanning the remaining n 1 elements and so on, for
(n 1) + (n 2) + ... + 2 + 1 = n(n 1) / 2 (n2) comparisons (see arithmetic progression).
Each of these scans requires one swap for n 1 elements (the final element is already in place).
Variants
Heapsort greatly improves the basic algorithm by using an implicit heap data structure to speed
up finding and removing the lowest datum. If implemented correctly, the heap will allow finding
the next lowest element in (log n) time instead of (n) for the inner loop in normal selection
sort, reducing the total running time to (n log n).
A bidirectional variant of selection sort, called cocktail sort, is an algorithm which finds both the
minimum and maximum values in the list in every pass. This reduces the number of scans of the
list by a factor of 2, eliminating some loop overhead but not actually decreasing the number of
comparisons or swaps. Note, however, that cocktail sort more often refers to a bidirectional
variant of bubble sort.
Selection sort can be implemented as a stable sort. If, rather than swapping in step 2, the
minimum value is inserted into the first position (that is, all intervening items moved down), the
algorithm is stable. However, this modification either requires a data structure that supports
efficient insertions or deletions, such as a linked list, or it leads to performing (n2) writes.
In the bingo sort variant, items are ordered by repeatedly looking through the remaining items to
find the greatest value and moving all items with that value to their final location.[1] Like
counting sort, this is an efficient variant if there are many duplicate values. Indeed, selection sort
does one pass through the remaining items for each item moved. Bingo sort does one pass for
each value (not item): after an initial pass to find the biggest value, the next passes can move
every item with that value to its final location while finding the next value as in the following
pseudocode (arrays are zero-based and the for-loop includes both the top and bottom limits,
as in Pascal):
bingo(array A)
{ This procedure sorts in ascending order. }
begin
max := length(A)-1;
{ The first iteration is written to look very similar to the subsequent
ones, but
without swaps. }
nextValue := A[max];
for i := max - 1 downto 0 do
if A[i] > nextValue then
nextValue := A[i];
while (max > 0) and (A[max] = nextValue) do
max := max - 1;
while max > 0 do begin
value := nextValue;
nextValue := A[max];
for i := max - 1 downto 0 do
if A[i] = value then begin
swap(A[i], A[max]);
max := max - 1;
end else if A[i] > nextValue then
nextValue := A[i];
while (max > 0) and (A[max] = nextValue) do
max := max - 1;
end;
end;
Thus if on average there are more than two items with each value, bingo sort can be expected to
be faster, because it executes the inner loop fewer times than selection sort