SORTING
Recognize EAN-13 In JavaUsing Barcode Control SDK for Java Control to generate, create, read, scan barcode image in Java applications.
EAN-13 Generation In JavaUsing Barcode encoder for Java Control to generate, create EAN 13 image in Java applications.
[CHAP. 14
Scanning EAN-13 In JavaUsing Barcode decoder for Java Control to read, scan read, scan image in Java applications.
Print Bar Code In JavaUsing Barcode printer for Java Control to generate, create bar code image in Java applications.
a[3] 99 66
Recognizing Bar Code In JavaUsing Barcode decoder for Java Control to read, scan read, scan image in Java applications.
Paint EAN13 In C#.NETUsing Barcode creation for .NET framework Control to generate, create EAN-13 image in .NET applications.
a[4] 66 99 33
Draw GTIN - 13 In Visual Studio .NETUsing Barcode generator for ASP.NET Control to generate, create EAN13 image in ASP.NET applications.
GS1 - 13 Creation In VS .NETUsing Barcode maker for .NET Control to generate, create EAN / UCC - 13 image in .NET applications.
a[5] 33
EAN-13 Supplement 5 Creator In Visual Basic .NETUsing Barcode generator for .NET framework Control to generate, create EAN 13 image in .NET applications.
GS1-128 Generation In JavaUsing Barcode drawer for Java Control to generate, create UCC.EAN - 128 image in Java applications.
a[6] 22
Paint GS1 DataBar Limited In JavaUsing Barcode creator for Java Control to generate, create GS1 DataBar Expanded image in Java applications.
UCC.EAN - 128 Creator In JavaUsing Barcode creation for Java Control to generate, create EAN / UCC - 14 image in Java applications.
a[7] 88
Paint Leitcode In JavaUsing Barcode drawer for Java Control to generate, create Leitcode image in Java applications.
Decode Code 3 Of 9 In NoneUsing Barcode decoder for Software Control to read, scan read, scan image in Software applications.
a[8] 77
ANSI/AIM Code 39 Maker In NoneUsing Barcode drawer for Software Control to generate, create Code 39 image in Software applications.
Bar Code Maker In .NET FrameworkUsing Barcode creation for ASP.NET Control to generate, create bar code image in ASP.NET applications.
99 22
UPC-A Supplement 5 Creator In NoneUsing Barcode maker for Font Control to generate, create UPCA image in Font applications.
Making GTIN - 13 In .NETUsing Barcode creator for ASP.NET Control to generate, create European Article Number 13 image in ASP.NET applications.
99 88
UPC - 13 Creator In NoneUsing Barcode drawer for Software Control to generate, create EAN-13 image in Software applications.
1D Barcode Drawer In .NET FrameworkUsing Barcode maker for Visual Studio .NET Control to generate, create Linear image in Visual Studio .NET applications.
99 77
88 33
88 22
88 77 77 88 88
33 33 33 22 44 22 33 55 22 44
66 22 55
d. Trace of the selection sort:
a[0] 44 22 a[1] 88 33 44 55 77 88 a[2] 55 a[3] 99 a[4] 66 a[5] 33 88 55 99 88 99 a[6] 22 44 a[7] 88 a[8] 77
e. Trace of the insertion sort:
a[0] 44 a[1] 88 55 44 33 a[2] 55 88 66 55 44 a[3] 99 88 66 55 a[4] 66 99 88 66 a[5] 33 a[6] 22 a[7] 88 a[8] 77
33 22
99 88 77
99 88 88
99 88
f. Trace of the merge sort:
a[0] 44 44 a[1] 88 55 a[2] 55 77 a[3] 99 99 a[4] 66 33 22 66 a[5] 33 66 33 77 66 88 77 77 88 88 88 99 a[6] 22 a[7] 88 a[8] 77
CHAP. 14]
SORTING
public static void sort(int[] a) { for (int i = a.length-1; i > 0; i--) { for (int j = 0; j < i; j++) { if (a[j] > a[j+1]) { swap(a, j, j+1); } } } } public static void sort(int[] a) { boolean sorted=false; int i = a.length-1; while (i > 0 && !sorted) { for (int j = 0; j < i; j++) { sorted = true; if (a[j] > a[j+1]) { swap(a, j, j+1); sorted = false; } } --i; } }
The loop invariant can be used to prove that the bubble sort does indeed sort the array. After the first iteration of the main i loop, the largest element must have moved to the last position. Wherever it began, it had to be moved step by step all the way to the right, because on each comparison the larger element is moved right. For the same reason, the second largest element must have been moved to the second-from-last position in the second iteration of the main i loop. So the two largest elements are in the correct locations. This reasoning verifies that the loop invariant is true at the end of every iteration of the main i loop. But then, after the last iteration, the n-1 largest elements must be in their correct locations. That forces the nth largest (i.e., the smallest) element also to be in its correct location, so the array must be sorted. The complexity function O(n2) means that, for large values of n, the number of loop iterations tends to be proportional to n2. That means that, if one large array is twice the size of another, it should take about four times as long to sort. The inner j loop iterates n 1 times on the first iteration of the outside i loop, n 2 times on the second iteration of the i loop, n 3 times on the third iteration of the i loop, and so on. For example, when n = 7, there are six comparisons made on the first iteration of the i loop, five comparisons made on the second iteration of the i loop, four comparisons made on the third iteration of the i loop, and so forth, so the total number of comparisons is 6 + 5 + 4 + 3 + 2 + 1 = 21. In general, the total number of comparisons will be (n 1) + (n 2) + (n 3) + + 3 + 2 + 1. This sum is n(n 1)/2. (See Theorem A.7 on page 323.) For large values of n, that expression is nearly n2/2 which is proportional to n2.
public static void sort(int[] a) { boolean sorted=false; for (int i = a.length; i > 0; i -= 2) { for (int j = 1; j < i; j++) { if (a[j-1] > a[j]) { swap(a,j-1,j); } } for (int j = i-2; j > 0; j--) { if (a[j-1] > a[j]) { swap(a, j-1, j); } } } }