You are on page 1of 12

Data Structure

Two Mark Questions….
1) What is sorting?
Sorting is arranging or ordering the data in some logical order either increasing or decreasing.
2) What are the fundamental types of sorting?
i) Internal Sorting
ii) External Sorting
3) What is internal sorting?
If the data to be sorted remains in main memory and also the sorting is carried out in main
memory it is called internal sorting.
4) What is external sorting?
If the data resides in auxiliary memory and is brought into main memory in blocks for sorting and
then result is returned back to auxiliary memory is called external sorting.
5) What is stable sorting?
Sorting technique is said to be stable, if for all records i and j such that k[I]=k[j], if r[i] precedes
r[j] in original file, r[i] precedes r[j] in sorted file also.
6) What is sorting by address?
In sorting the auxiliary table pointers may be used so that the pointers are moved instead of
actual data. This reduces the time needed for sorting.
7) What are the factors to be considered while selecting a sorting method?
Three factors are:
i) Length of time for the programmer to code the sorting program.
ii) Amount of machine time necessary for running the program.
iii) Amount of space necessary for the program to execute.
8) List the names of some internal sorting methods.
i) Bubble sort
ii) Insertion sort
iii) Selection sort
iv) Shell sort
v) Quick sort
vi) Heap sort
9) Write about any one external sorting method?

iii) Increment down pointer. 15) What is straight selection sort or push down sort? Straight selection sort is an in-place sort. until x[down]>a iv) Decrement up pointer. ii) The mean sort uses the mean of the sub file as pivot for sorting. 10) What is bubble sort? Bubble sort arranges the array of integers as x[i]<=x[j].Merge sort is an example of external sorting. 16) What is binary tree sort? In binary tree sort. until x[up]<=a v) If up>down. 13) What are the different methods for choosing a pivot? i) The median of the first. interchange x[down] with x[up] vi) If up<=down. Then the sorted sub arrays are brought in to main memory for merging and sorting. 17) What is Heap sort? Heap sort is also an in-place sort. with the following rules: i) Select a pivot element. 12) What is Pivot? The element around which a file is partitioned is called a pivot. interchange x[up] with pivot. a binary tree of descending priority queue is created first and inorder traversing of the tree gives a sorted list. from which the largest element should be chosen and interchanged with the element at end. 14) What is selection sort? Selection sort is one in which successive elements are sorted in order and placed into their proper sorted position. . 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. last and middle elements of the sub file to be sorted can be chosen as pivot. iii) Bsort uses the middle element of subfile as pivot. This sorts the sub arrays and the sorted sub arrays are stored in an auxiliary table. which creates a binary search tree in which elements are to be sorted and stored. from the array x. ii) Initialize the down and up as the lower bound and upper bound of array. 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.

Although the algorithm is simple. Because it only uses comparisons to operate on elements.> </j<=n-1. The pass through the list is repeated until no swaps are needed. the free encyclopedia Jump to: navigation. is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted.></j Bubble sort From Wikipedia. comparing each pair of adjacent items and swapping them if they are in the wrong order. sometimes incorrectly referred to as sinking sort. most other algorithms are more efficient for sorting large lists. search Bubble sort.19) What is Ascending heap/Min heap? Min heap is an almost complete binary tree such that the content of each node is greater than or equal to the content of its father. it is a comparison sort. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Bubble sort Class Data structure Worst case performance Best case performance Sorting algorithm Array . 20) What is the inequality/ condition for sequential representation of heap? Info[j]<=info[(j-1)/2] for 0<=((j-1)/2)<j<=n-1. which indicates that the list is sorted.

. but not insertion sort. and then reverses itself. Even other О(n2) sorting algorithms. is that the ability to detect that the list is sorted is efficiently built into the algorithm. not only does insertion sort have this mechanism too. tend to have better performance than bubble sort. Its average speed is comparable to faster algorithms like quicksort. as they are quickly swapped. sometimes incorrectly referred to as sinking sort. However. such as insertion sort. bubble sort is not a practical sorting algorithm when n is large. Small elements towards the end. It can move turtles fairly well. and can move turtles extremely quickly before proceeding to smaller and smaller gaps to smooth out the list. This has led to these types of elements being named rabbits and turtles. Because it only uses comparisons to operate on elements. which indicates that the list is sorted. but it also performs better on a list that is substantially sorted (having a small number of inversions). Although the algorithm is simple. Various efforts have been made to eliminate turtles to improve upon the speed of bubble sort. going end to beginning. Performance Bubble sort has worst-case and average complexity both О(n2). The only significant advantage that bubble sort has over most other implementations. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. even those with better average-case complexity. most other algorithms are more efficient for sorting large lists. even quicksort. comparing each pair of adjacent items and swapping them if they are in the wrong order. where n is the number of items being sorted. Performance of bubble sort over an already-sorted list (best-case) is O(n). Therefore. By contrast. Cocktail sort is a bi-directional bubble sort that goes from beginning to end. perform their entire sorting process on the set and thus are more complex.Average case performance Worst case space complexity auxiliary Bubble sort. move to the beginning extremely slowly. respectively. most other algorithms. is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted. Large elements at the beginning of the list do not pose a problem. The pass through the list is repeated until no swaps are needed. it is a comparison sort. however. Rabbits and turtles The positions of the elements in bubble sort will play a large part in determining its performance. but it retains O(n2) worst-case complexity. Comb sort compares elements separated by large gaps. There exist many sorting algorithms with substantially better worst-case or average complexity of O(n log n).

elements written in bold are being compared. and swaps since 5 > 1. The algorithm needs one whole pass without any swap to know it is sorted.Step-by-step example Let us take the array of numbers "5 1 4 2 8". In each step. Here. Now. First Pass: ( 5 1 4 2 8 ) ( 1 5 4 2 8 ). Second Pass: (14258) (14258) ( 1 4 2 5 8 ) ( 1 2 4 5 8 ). since these elements are already in order (8 > 5). and sort the array from lowest number to greatest number using bubble sort. algorithm compares the first two elements. algorithm does not swap them. the array is already sorted. Three passes will be required. Swap since 5 > 2 ( 1 4 2 5 8 ) ( 1 4 2 5 8 ). Third Pass: (12458) (12458) (12458) (12458) (12458) (12458) (12458) (12458) Implementation Pseudocode implementation The algorithm can be expressed as (0-based array): procedure bubbleSort( A : list of sortable items ) repeat swapped = false for i = 1 to length(A) . Swap since 4 > 2 (12458) (12458) (12458) (12458) Now. ( 1 5 4 2 8 ) ( 1 4 5 2 8 ). Swap since 5 > 4 ( 1 4 5 2 8 ) ( 1 4 2 5 8 ). but our algorithm does not know if it is completed. A[i] ) swapped = true end if end for until not swapped end procedure .1 inclusive do: /* if this pair is out of order */ if A[i-1] > A[i] then /* swap them and remember something changed */ swap( A[i-1].

1 until not swapped end procedure More generally. This allows us to skip over a lot of the elements. . In particular. all elements after the last swap are sorted. 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.Optimizing bubble sort 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. after every pass. 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]. 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]. it can happen that more than one element is placed in their final position on a single pass. and do not need to be checked again. A[i]) swapped = true end if end for n = n . A[i]) newn = i end if end for n = newn until n = 0 end procedure Alternate modifications. So. resulting in about a worst case 50% improvement in comparison count (though no improvement in swap counts).

with smaller elements taking longer to move to their correct positions. a sorting algorithm that continuously steps through a list. in his famous book The Art of Computer Programming. Experimental results such as those of Astrachan have also shown that insertion sort performs considerably better even on random lists. to introductory computer science students.[3] Bubble sort is asymptotically equivalent in running time to insertion sort in the worst case. some of which he then discusses. Due to its simplicity. which famously calls bogosort "the archetypical perversely awful algorithm". For these reasons many modern algorithm textbooks avoid using the bubble sort algorithm in favor of insertion sort. also calls bubble sort "the generic bad algorithm". concluded that "the bubble sort seems to have nothing to recommend it. its O(n2) complexity means that its efficiency decreases dramatically on lists of more than a small number of elements. some researchers such as Owen Astrachan have gone to great lengths to disparage bubble sort and its continued popularity in computer science education. algorithms like insertion sort are usually considerably more efficient. but the two algorithms differ greatly in the number of swaps necessary. except a catchy name and the fact that it leads to some interesting theoretical problems". Note that the largest end gets sorted first. However.[1] The Jargon file. or a sorting algorithm.In practice A bubble sort. Although bubble sort is one of the simplest sorting algorithms to understand and implement. . bubble sort is often used to introduce the concept of an algorithm. Even among simple O(n2) sorting algorithms. swapping items until they appear in the correct order.[2] Donald Knuth. recommending that it no longer even be taught.

Selection sort is noted for its simplicity. and asymptotically more branch mispredictions. For example. 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. Find the minimum value in the list 2.Bubble sort also interacts poorly with modern CPU hardware. twice as many cache misses. 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) .[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). particularly where auxiliary memory is limited. and generally performs worse than the similar insertion sort. and the sublist of items remaining to be sorted. and also has performance advantages over more complicated algorithms in certain situations. specifically an in-place comparison sort. It requires at least twice as many writes as insertion sort. a selection sort is a sorting algorithm. Repeat the steps above for the remainder of the list (starting at the second position and advancing each time) Effectively. it is used in a polygon filling algorithm. Swap it with the value in the first position 3. occupying the remainder of the array. the list is divided into two parts: the sublist of items already sorted. Algorithm The algorithm works as follows: 1. making it inefficient on large lists. It has O(n2) time complexity. Selection sort In computer science. 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. which is built up from left to right and is found at the beginning.

Selection sort can also be used on list structures that make add and remove efficient. 2. 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++) { /* find the min element in the unsorted a[j . 3. remember its index */ iMin = i. i++) { /* if this element is less. .j. i < n. /* test against elements after j to find the smallest */ for ( i = j+1. } } /* iMin is the index of the minimum element. } } Mathematical definition Let be a non-empty set and 1. and then insert it at the end of the values sorted so far. In this case it is more common to remove the minimum element from the remainder of the list. a[iMin]). .. int iMin. such as a linked list. is a permutation of for all such that where: . and . /* advance the position through the entire array */ /* (could do j < n-1 because single element is also min element) */ for (j = 0. then it is the new minimum */ if (a[i] < a[iMin]) { /* found new minimum. Swap it with the current position */ if ( iMin != j ) { swap(a[j]. n-1] */ /* assume the min is the first element */ iMin = j. j < n-1.

This can be important if writes are significantly more expensive than reads. selection sort almost always outperforms bubble sort and gnome sort. Each of these scans requires one swap for n − 1 elements (the final element is already in place).e. while insertion sort's running time can vary considerably.. However. + 2 + 1 = n(n − 1) / 2 ∈ Θ(n2) comparisons (see arithmetic progression). such as with EEPROM or Flash memory. the first k elements in the array are in sorted order. insertion sort or selection sort are both typically faster for small arrays (i. However. is the smallest element of 5.. as cycle sort is theoretically optimal in the number of writes. for (n − 1) + (n − 2) + . . Simple calculation shows that insertion sort will therefore usually perform about half as many comparisons as selection sort." While selection sort is preferable to insertion sort in terms of number of writes (Θ(n) swaps versus Ο(n2) swaps). although it can perform just as many or far fewer depending on the order the array was in prior to sorting. where every write lessens the lifespan of the memory. A useful optimization in practice for the recursive algorithms is to switch to insertion sort or selection sort for "small enough" sublists. while selection sort must scan all remaining elements to find the k + 1st element. Insertion sort's advantage is that it only scans as many elements as it needs in order to place the k + 1st element. 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. is the set of elements of . this is more often an advantage for insertion sort in that it runs much more efficiently if the array is already sorted or "close to sorted. Finally. It can be seen as an advantage for some real-time applications that selection sort will perform identically regardless of the order of the array. Comparison to other sorting algorithms Among simple average-case Θ(n2) algorithms. and without one instance of the smallest element of .4. Selecting the lowest element requires scanning all n elements (this takes n − 1 comparisons) and then swapping it into the first position. fewer than 10–20 elements). selection sort is greatly outperformed on larger arrays by Θ(n log n) divide-and-conquer algorithms such as mergesort. Insertion sort is very similar in that after the kth iteration. Finding the next lowest element requires scanning the remaining n − 1 elements and so on. it almost always far exceeds (and never beats) the number of writes that cycle sort makes.

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. In the bingo sort variant. that cocktail sort more often refers to a bidirectional variant of bubble sort. while (max > 0) and (A[max] = nextValue) do max := max . } nextValue := A[max]. If. is an algorithm which finds both the minimum and maximum values in the list in every pass. the algorithm is stable.1 downto 0 do if A[i] = value then begin . reducing the total running time to Θ(n log n). Indeed. { The first iteration is written to look very similar to the subsequent ones. eliminating some loop overhead but not actually decreasing the number of comparisons or swaps. the minimum value is inserted into the first position (that is. However.Variants Heapsort greatly improves the basic algorithm by using an implicit heap data structure to speed up finding and removing the lowest datum. rather than swapping in step 2. all intervening items moved down). however. Note. but without swaps. } begin max := length(A)-1. the heap will allow finding the next lowest element in Θ(log n) time instead of Θ(n) for the inner loop in normal selection sort.1. 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. called cocktail sort. Bingo sort does one pass for each value (not item): after an initial pass to find the biggest value.1 downto 0 do if A[i] > nextValue then nextValue := A[i]. for i := max . Selection sort can be implemented as a stable sort. This reduces the number of scans of the list by a factor of 2. A bidirectional variant of selection sort. as in Pascal): bingo(array A) { This procedure sorts in ascending order. or it leads to performing Θ(n2) writes. such as a linked list. nextValue := A[max].[1] Like counting sort. this modification either requires a data structure that supports efficient insertions or deletions. while max > 0 do begin value := nextValue. selection sort does one pass through the remaining items for each item moved. If implemented correctly. for i := max . this is an efficient variant if there are many duplicate values.

swap(A[i]. end. end else if A[i] > nextValue then nextValue := A[i].1.1. while (max > 0) and (A[max] = nextValue) do max := max . Thus if on average there are more than two items with each value. max := max . because it executes the inner loop fewer times than selection sort . end. A[max]). bingo sort can be expected to be faster.