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
Suppose we are sorting a large number of local phone numbers, for example, all residential phone numbers in the 412 area code region (about 1 million) We sort the numbers without use of comparisons in the following way. Create an a bit array of size 107. It takes about 1Mb. Set all bits to 0. For each phone number turn-on the bit indexed by that phone number. Finally, walk through the array and for each bit 1 record its index, which is a phone number.
We immediately see two drawbacks to this sorting algorithm. Firstly, we must know how to handle duplicates. Secondly, we must know the maximum value in the unsorted array.. Thirdly, we must have enough memory - it may be impossible to declare an array large enough on some systems.
The first problem is solved by using linked lists, attached to each array index. All duplicates for that bucket will be stored in the list. Another possible solution is to have a counter. As an example let us sort
Moving through the array we increment counters:
Next,we simply read off the number of each occurrence:
Example. Here is one step of the algorithm. The largest element - 7 - is bubbled to the top:
ANOTHER PROGRAM ON BUBBLE SORT
ANOTHER CODE FOR BUBBLE SORT
- Bucket sort
- Bubble sort
- Insertion sort
- Selection sort
- Heapsort
- Mergesort
O(n) algorithms
Bucket Sort
Suppose we need to sort an array of positive integers {3,11,2,9,1,5}. A bucket sort works as follows: create an array of size 11. Then, go through the input array and place integer 3 into a second array at index 3, integer 11 at index 11 and so on. We will end up with a sorted list in the second array.Suppose we are sorting a large number of local phone numbers, for example, all residential phone numbers in the 412 area code region (about 1 million) We sort the numbers without use of comparisons in the following way. Create an a bit array of size 107. It takes about 1Mb. Set all bits to 0. For each phone number turn-on the bit indexed by that phone number. Finally, walk through the array and for each bit 1 record its index, which is a phone number.
We immediately see two drawbacks to this sorting algorithm. Firstly, we must know how to handle duplicates. Secondly, we must know the maximum value in the unsorted array.. Thirdly, we must have enough memory - it may be impossible to declare an array large enough on some systems.
The first problem is solved by using linked lists, attached to each array index. All duplicates for that bucket will be stored in the list. Another possible solution is to have a counter. As an example let us sort
3, 2, 4, 2, 3, 5
. We start with an array of 5 counters set to zero.0 | 1 | 2 | 3 | 4 | 5 |
0 | 0 | 0 | 0 | 0 | 0 |
0 | 1 | 2 | 3 | 4 | 5 |
0 | 0 | 2 | 2 | 1 | 1 |
2 2 3 3 4 5
.O(n2) algorithms
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 bubbleSort(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; } } } }
7, 5, 2, 4, 3, 9The worst-case runtime complexity is O(n2). See explanation below
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
Java program to bubble sort: This code sorts numbers inputted by user using Bubble sort algorithm.
Java programming code
Complexity of bubble sort is O(n2) which makes it a less frequent option for arranging in sorted order when quantity of numbers is high.
In bubble sort algorithm, array is traversed from first element to last element. Here, current element is compared with the next element. If current element is greater than the next element, it is swapped.
Output:
Array Before Bubble Sort 3 60 35 2 45 320 5 Array After Bubble Sort 2 3 5 35 45 60 320
ANOTHER CODE FOR BUBBLE SORT
| ||
|
No comments:
Post a Comment