Using Bubble Sort in Java
Using Bubble Sort QR Code JIS X 0510 Generation In Java Using Barcode drawer for Java Control to generate, create QR Code image in Java applications. The simplest way to sort any amount of data is to start at the beginning and compare the first two adjacent items So if you need to sort a list of numbers, you compare the first number with the second number If the first number is bigger, you swap its place with the second number If the second number is bigger, you don t do anything at all After comparing the first two items, you move down to comparing the second and third items, and so on Repetitively comparing two adjacent items is the basic idea behind the bubble sort algorithm, as shown in Figure 1-1 The bubble sort algorithm gets its name because small values tend to bubble up to the top Create Bar Code In Java Using Barcode generator for Java Control to generate, create bar code image in Java applications. Using Bubble Sort
Bar Code Decoder In Java Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications. Original list
Encoding QR Code In C#.NET Using Barcode generation for .NET framework Control to generate, create QR Code ISO/IEC18004 image in Visual Studio .NET applications. Bubble sort compares the first two items and sorts them
QR Code 2d Barcode Printer In .NET Using Barcode printer for Visual Studio .NET Control to generate, create QR Code JIS X 0510 image in .NET applications. Bubble sort compares the next two adjacent items If they are already sorted, then nothing happens
Denso QR Bar Code Drawer In Visual Basic .NET Using Barcode creator for Visual Studio .NET Control to generate, create QR image in VS .NET applications. After reaching the end of the list, bubble sort must go back through the list again
UPC-A Supplement 5 Generator In Java Using Barcode generation for Java Control to generate, create GS1 - 12 image in Java applications. Figure 1-1: A bubble sort repetitively compares two adjacent items
Encode Code-128 In Java Using Barcode printer for Java Control to generate, create Code128 image in Java applications. List is finally sorted
Code-39 Generation In Java Using Barcode encoder for Java Control to generate, create ANSI/AIM Code 39 image in Java applications. Basically the bubble sort algorithm works like this: Barcode Maker In Java Using Barcode maker for Java Control to generate, create barcode image in Java applications. 1 Compare two adjacent items 2 Swap the two items if necessary 3 Repeat Steps 1 and 2 with each pair of adjacent items 4 Repeat Steps 1 3 to examine the entire list again until no swapping Generating EAN / UCC - 13 In Java Using Barcode printer for Java Control to generate, create EAN / UCC - 13 image in Java applications. occurs and then the list is sorted Although the bubble sort algorithm is easy to implement, it s also the slowest and most inefficient algorithm because it must examine an entire list multiple times For sorting a small list of pre-sorted data, the bubble sort algorithm works efficiently For sorting large lists of unsorted data, any other sorting algorithm is much faster than the bubble sort algorithm, as shown in Figure 1-2 Paint Identcode In Java Using Barcode maker for Java Control to generate, create Identcode image in Java applications. Book IV 1
Making UPC A In C# Using Barcode creator for VS .NET Control to generate, create UPC-A Supplement 2 image in VS .NET applications. Sorting Algorithms
Paint Universal Product Code Version A In VB.NET Using Barcode generation for .NET Control to generate, create UPC Code image in VS .NET applications. Using Selection Sort
Code-128 Printer In Visual Basic .NET Using Barcode generator for .NET Control to generate, create Code 128B image in Visual Studio .NET applications. With a short list, bubble sort can work quickly
Reading Bar Code In Java Using Barcode reader for Java Control to read, scan read, scan image in Java applications. Step #1 74 32 21 Code128 Scanner In VS .NET Using Barcode reader for .NET Control to read, scan read, scan image in .NET applications. Step #2 9 With a long, unsorted list, bubble sort takes too much time
Encode Code-128 In C# Using Barcode maker for Visual Studio .NET Control to generate, create USS Code 128 image in Visual Studio .NET applications. Step #1 Decode Data Matrix ECC200 In .NET Framework Using Barcode decoder for .NET framework Control to read, scan read, scan image in .NET framework applications. Step #5 Making Code 128 Code Set A In Visual Studio .NET Using Barcode generation for VS .NET Control to generate, create Code 128 Code Set B image in .NET applications. Step #2 Figure 1-2: The bubble sort algorithm examines the entire list of data several times
Step #6 Step #3 Final sorted list
Step #4 Using Selection Sort
Another simple way to sort a list is to search the entire list until you find the smallest value Then move that value to the front of the list Now repeat the process all over again, skipping the first item By repetitively searching for the smallest item and moving it to the front of the list, the selection sort algorithm can eventually sort an entire list, as shown in Figure 1-3 The selection sort algorithm works like this: 1 Find the smallest item in a list 2 Swap this value with the value currently at the front of the list 3 Repeat Steps 1 and 2 with the current size of the list minus one (list size = list size 1) Using Insertion Sort
Unsorted list
The smallest number of the list (9) gets swapped with the first number of the list (32) Figure 1-3: Selection sort repetitively moves the smallest value to the front of the list
The smallest number (21) of the remaining unsorted items gets swapped with the first number of the unsorted list (32) The smallest number (32) of the remaining unsorted items gets swapped with the first number of the unsorted list (74) Final sorted list
For sorting small lists, selection sort is fast, but for sorting large lists, selection sort needs too much time progressively examining smaller sections of a list Despite this drawback with sorting large lists, selection sort is popular because it s simple to implement Using Insertion Sort
The insertion sort algorithm acts like a cross between the bubble sort and the selection sort algorithm Like bubble sort, insertion sort examines two adjacent values Like selection sort, insertion sort moves smaller values from their current location to an earlier position near the front of the list The insertion sort algorithm works like this: Book IV 1
1 Start with the second item in the list 2 Compare this second item with the first item If the second value is smaller, swap places in the list
Sorting Algorithms
3 Compare the next item in the list and insert it in the proper place in
relation to the previously sorted items
4 Repeat Step 3 until the entire list is sorted
The main difference between insertion sort and selection sort is that the selection sort only swaps two adjacent values whereas insertion sort can move a value to a non-adjacent location, as shown in Figure 1-4 Using Shell Sort
32 9 32 Unsorted list
Each low value is yanked out of the list
9 21 9 and inserted in its proper, sorted location at the beginning of the list
Figure 1-4: Insertion sort only examines a list once to sort it
Final sorted list
One major advantage of the insertion sort algorithm is that it only needs to examine an entire list once to sort it In comparison, bubble sort must repetitively examine the entire list multiple times, and selection sort must repetitively examine progressively smaller lists multiple times As a result, the insertion sort algorithm is much faster while being easy to implement as well
|
|
|
|
| ALL RIGHTS RESERVED. Business Refinery (c) 2006 - 2010. | Terms of Use | Privacy Policy |