Imran Rasool Bbe-1586
Imran Rasool Bbe-1586
Imran Rasool Bbe-1586
ASSIGNMENT - 1
Sorting of Algorithm
Presented by IMRAN RASOOL
Roll Number BBE-1586
Introduction
In other words, a sorted array is an array that is in a particular order. For example,
[a,b,c, d] is sorted alphabetically,[1,2,3,4,5] is a list of integers sorted in increasing
order, and [5,4,3,2,1] is a list of integers sorted in decreasing order.
A sorting algorithm takes an array as input and outputs a permutation of that array
that is sorted.
There are two broad types of sorting algorithms: integer sorts and comparison sorts.
Sorting is ordering a list of objects. We can distinguish two types of sorting. If the
number of objects is small enough to fits into the main memory, sorting is called
internal sorting. If the number of objects is so large that some of them reside on
external storage during the sort, it is called external sorting. In this chapter we
consider the following internal sorting algorithms
Bucket sort
Bubble sort
Insertion sort
Selection sort
Heapsort
Merge sort
Bucket Sort
Bubble Sort
The algorithm works by comparing each item in the list with the item next to it, and
swapping them if required. In other words, the largest element has bubbled to the top
of the array. The algorithm repeats this process until it makes a pass all the way
through the list without swapping any items.
void bubble Sort (int ar[]){ for (int i = (ar.length - 1); i >= 0; i--) { for (int j = 1; j
≤ i; j++) { if (ar[j-1] > ar[j]) { int temp = ar[j-1]; ar[j-1]
= ar[j]; ar[j] = temp; } } } }
Example. Here is one step of the algorithm. The largest element - 7 - is bubbled to
the top:
7, 5, 2, 4, 3, 9
5, 7, 2, 4, 3, 9
5, 2, 7, 4, 3, 9
5, 2, 4, 7, 3, 9
5, 2, 4, 3, 7, 9
5, 2, 4, 3, 7, 9
The worst-case runtime complexity is O(n2). See explanation below
Selection Sort
The algorithm works by selecting the smallest unsorted item and then swapping it
with the item in the next position to be filled.
The selection sort works as follows: you look through the entire array for the smallest
element, once you find it you swap it (the smallest element) with the first element of
the array. Then you look for the smallest element in the remaining array (an array
without the first element) and swap it with the second element. Then you look for the
smallest element in the remaining array (an array without first and second elements)
and swap it with the third element, and so on. Here is an example
,
void selection Sort(int[] ar){ for (int i = 0; i ‹ ar.length-1; i++) { int min = i;
for (int j = i+1; j ‹ ar.length; j++) if (ar[j] ‹ ar[min]) min = j; int temp =
ar[i]; ar[i] = ar[min]; ar[min] = temp;} }
Example.
Insertion Sort
To sort unordered list of elements, we remove its entries one at a time and then
insert each of them into a sorted part (initially empty):
void insertionSort(int[] ar){ for (int i=1; i ‹ ar.length; i++) { int index = ar[i]; int j
= i; while (j > 0 && ar[j-1] > index) { ar[j] = ar[j-1]; j--; }
ar[j] = index;} }
Example. We color a sorted part in green, and an unsorted part in black. Here is an
insertion sort step by step. We take an element from unsorted part and compare it
with elements in sorted part, moving form right to left.
Let us compute the worst-time complexity of the insertion sort. In sorting the most
expensive part is a comparison of two elements. Surely that is a dominant factor in
the running time. We will calculate the number of comparisons of an array of N
elements:
we need 0 comparisons to insert the first element
we need 1 comparison to insert the second element
we need 2 comparisons to insert the third element
...
we need (N-1) comparisons (at most) to insert the last element
Totally,
1 + 2 + 3 + ... + (N-1) = O(n2)
The worst-case runtime complexity is O(n2).What is the best-case runtime complexity?
O(n). The advantage of insertion sort comparing it to the previous two sorting
algorithm is that insertion sort runs in linear time on nearly sorted data.
Merge Sort
Lower bound
What is the lower bound (the least running time in the worst-case) for all sorting
comparison algorithms? A lower bound is a mathematical argument saying you can't
hope to go faster than a certain amount. The preceding section presented O(n log n)
merge sort, but is this the best we can do? In this section we show that any sorting
algorithm that sorts using comparisons must make O(n log n) such comparisons.
Suppose we have N elements. How many different arrangements can you make? There
are N possible choices for the first element, (N-1) possible choices for the second
element, .. and so on. Multiplying them, we get N! (N factorial.)
Next, we observe that each comparison cut down the number of all possible
comparisons by a factor 2. Any comparison sorting algorithm can always be put in the
form of a decision tree. And conversely, a tree like this can be used as a sorting
algorithm. This figure illustrates sorting a list of {a1, a2, a3} in the form of a decision
tree:
Observe, that the worst-case number of comparisons made by an algorithm is just the
longest path in the tree. At each leaf in the tree, no more comparisons to be made.
Therefore, the number of leaves cannot be more than 2x, where x is the maximum
number of comparisons (or the longest path in the tree). On the other hand, as we
counted in the previous paragraph, the number of all possible permutations is n!.
Combining these two facts, gives us the following equality:
2x ≥ N!
where x is the number of comparisons. By taking logarithm, implies
x ≥ log N!
Using the Stirling formula for N!, we finally arrive at
x ≥ N log N
or
x = O(N Log N)
Sorting in Java
In this section we discuss four different ways to sort data in Java.
Arrays of primitives