Web Notes

2016.08.20

### Using Liquid in Jekyll - Live with Demos

Liquid is a simple template language that Jekyll uses to process pages for your site. With Liquid you can output complex contents without additional plugins.

rethink about sorting

Sorting plays a major role in commercial data processing and in modern scientific computing. Here, we consider several classical sorting methods.

One of the simplest sorting algorithms works as follows: First, find the smallest item in the array and exchange it with the first entry (itself if the first entry is already the smallest). Then find the next smallest item and exchange it with the second entry. Continue in this way until the entire array is sorted.

```
public class Selection {
public static void sort(Comparable[] a) {
// Sort a[] into increasing order.
int N = a.length;
for (int i = 0; i < N; i++) {
// Exchange a[i] with smallest entry in a[i+1...N]
int min = i;
for (int j = i+1; j < N; j++) {
if (less(a[j], a[min])) min = j;
}
exch(a, i, min);
}
}
private static boolean less(Comparable v, Comparable w) {
return v.compareTo(w) < 0;
}
private static void exch(Comparable[] a, int i, int j) {
Comparable t = a[i]; a[i] = a[j]; a[j] = t;
}
}
```

For each `i`

, this implementation puts the `i`

th smallest item in `a[i]`

. The entries to the left of position `i`

are the `i`

smallest items in the array and are not examined again.

For insertion sort in a computer implementation, we need to make space to insert the current item by moving larger items one position to the right, before inserting the current item into the vacated position.

The items to the left of the current index are in sorted order during the sort, but they are not in their final position, as they may have to be moved to make room for smaller items encountered later. The array is, however, fully sorted when the index reaches the right end. The running time of insertion sort depends on the initial order of the items in the input. For example, if the array if large and its entries are nearly in order, then insertion sort is much, much faster than if the entries are randomly ordered or in reverse order.

```
public class Insertion {
public static void sort(Comparable[] a) {
// Sort a[] into increasing order.
int N = a.length;
for (int i = 1; i < N; i++) {
// Insert a[i] among a[i-1], a[i-2], a[i-3]...
for (int j = i; j > 0 && less(a[j], a[j-1]); j--) {
exch(a, j, j-1);
}
}
}
private static boolean less(Comparable v, Comparable w) {
return v.compareTo(w) < 0;
}
private static void exch(Comparable[] a, int i, int j) {
Comparable t = a[i]; a[i] = a[j]; a[j] = t;
}
}
```

For each `i`

from 0 to N-1, exchange `a[i]`

with the entries that are smaller in `a[0]`

through `a[i-1]`

. As the index `i`

travels from left to right, the entries to its left are in sorted order in the array, so the array is fully sorted when `i`

reaches the right end.

More generally, we consider the concept of a *partially sorted* array, as follows: An *inversion* is a pair of entries that are out of order in the array. For instance, `E A X A M P L E`

has 11 inversions: `E-A`

, `X-A`

, `X-M`

, `X-P`

, `X-L`

, `X-E`

, `M-L`

, `M-E`

, `P-L`

, `P-E`

, and `L-E`

. If the number of inversions in an array is less than a constant multiple of partially sorted arrays are the following:

- An array where each entry is not far from its final position.
- A small array appended to a large sorted array.
- An array with only a few entries that are not in place.

Insertion sort is an efficient method for such arrays. Indeed, when the number of inversions is low, insertion sort is likely to be faster than any other sorting method in this post.

To exhibit the value of knowing properties of elementary sorts, we next consider a fast algorithm based on insertion sort. Insertion sort is slow for large unordered arrays because the only exchanges it does involve adjacent entries, so items can move through the array only one place at a time. For example, if the item with the smallest key happens to be at the end of the array, *N* - 1 exchanges are needed to get that one item where it belongs. *Shellsort* is a simple extension of insertion sort that gains speed by allowing exchanges of array entries that are far apart, to produce partially sorted arrays that can be efficiently sorted, eventually by insertion sort.

The idea is to rearrange the array to give it the property that taking every *h*th entry (starting anywhere) yields a sorted subsequence. Such an array is said to be *h*-sorted. Put another way, an *h*-sorted array is *h* independent sorted subsequences, interleaved together. By *h*-sorting for some large values of *h*, we can move items in the array long distances and thus make it easier to *h*-sort for smaller values of *h*. Using such a procedure for any sequence of values of *h* that ends in 1 will produce a sorted array: that is shellsort.

Onw way to implement shellsort would be, for each *h*, to use insertion sort independently on each of the *h* subsequences. Because the subsequences are independent, we can use an even simpler approach: when *h*-sorting the array, we insert each item among the previous items in its *h*-subsequence by exchanging it with those that have larger keys (moving them each one position to the right in the subsequence). We accomplish this task by using the insertion-sort code, but modified reduces the shellsort implementation to an insertion-sort-like pass through the array for each increment.

```
public class Shell {
public static void sort(Comparable[] a) {
// Sort a[] into increasing order.
int N = a.length;
int h = 1;
while (h < N/3) h = 3*h + 1; // 1, 4, 13, 40, 121, ...
while (h >= 1) {
// h-sort the array
for (int i = h; i < N; i++) {
// Insert a[i] among a[i-h], a[i-2*h], a[i-3*h]...
for (int j = i; j >= h && less(a[j], a[j-h]); j -= h) {
exch(a, j, j-h);
}
}
h = h/3;
}
}
private static boolean less(Comparable v, Comparable w) {
return v.compareTo(w) < 0;
}
private static void exch(Comparable[] a, int i, int j) {
Comparable t = a[i]; a[i] = a[j]; a[j] = t;
}
}
```

This implementation uses the sequence of decreasing values $\frac{1}{2} (3^k -1)$, starting at the largest increment less than *N*/3 and decreasing to 1.

How do we decide what increment sequence to use? In general, this question is a difficult one to answer. The performance of the algorithm depends not just on the number of increments, but also on arithmetical interactions among the increments such as the size of their common divisors and other properties. Many different increment sequences have been studied in the literature, but no provably best sequence has been found.

Shellsort is useful even for large arrays, particularly by contrast with selection sort and insertion sort. It also performs well on arrays that are in arbitrary order (not necessarily random). Experienced programmers sometimes choose shellsort because it has acceptable running time even for moderately large array; it requires a small amount of code; and it uses no…

THE END

Ads by Google

Frank Lin

Hey, there! This is Frank Lin (@flinhong), one of the 1.41 billion . This 'inDev. Journal' site holds the exploration of my quirky thoughts and random adventures through life. Hope you enjoy reading and perusing my posts.

Web Notes

2016.08.20

Liquid is a simple template language that Jekyll uses to process pages for your site. With Liquid you can output complex contents without additional plugins.

C Notes

2017.04.24

Understand the common sorting algorithms, just because recent take CS50 course.

JavaScript Notes

2018.12.17

JavaScript is a very function-oriented language. As we know, functions are first class objects and can be easily assigned to variables, passed as arguments, returned from another function invocation, or stored into data structures. A function can access variable outside of it. But what happens when an outer variable changes? Does a function get the most recent value or the one that existed when the function was created? Also, what happens when a function invoked in another place - does it get access to the outer variables of the new place?

Ads by Google