What is Library Sort
Library Sort is a sorting algorithm that works by dividing a list of items into smaller groups and recursively applying the same sorting process to those groups until the list is sorted. It's called "Library Sort" because it's similar to how books are sorted in a library.
Imagine you have a big pile of books that need to be sorted alphabetically. The first step would be to divide the books into smaller groups based on the first letter of their title. You would then sort each of those groups independently. For example, all the books starting with the letter "A" would be sorted together, all the books starting with the letter "B" would be sorted together, and so on.
Next, you would merge the sorted groups back together in alphabetical order, so all the books starting with "A" would come first, followed by all the books starting with "B," and so on. You would repeat this process until the entire pile of books was sorted.
Library Sort works in a similar way. It divides the list into smaller sub-lists, sorts each sub-list independently, and then merges them back together until the entire list is sorted. The advantage of Library Sort is that it can be very efficient for sorting large datasets, especially if the data is already partially sorted.
Who invented it?
The Library Sort algorithm was first described by two computer scientists named Peter McIlroy and Keith Bostic in a paper published in 1993. McIlroy is a well-known computer scientist who has made significant contributions to the development of programming languages, operating systems, and algorithms. He is particularly known for his work on the Unix operating system and the development of the C programming language. Bostic is also a respected computer scientist who has worked on many important software projects, including the Berkeley Software Distribution (BSD) Unix operating system. Together, they developed the Library Sort algorithm as a way to efficiently sort large datasets in the context of their work on a text retrieval system called WAIS (Wide Area Information Server).
Pseudocode
function librarySort(array)
// divide the array into subarrays
subarrays = divideArray(array)
// sort each subarray
for i = 0 to length(subarrays)-1 do
subarrays[i] = sort(subarrays[i])
// merge the subarrays back together
sortedArray = mergeArrays(subarrays)
return sortedArray
function divideArray(array)
// divide the array into subarrays based on the first letter of each element
subarrays = []
currentSubarray = []
currentLetter = array[0][0]
for i = 0 to length(array)-1 do
if array[i][0] == currentLetter then
// add the element to the current subarray
currentSubarray.append(array[i])
else
// start a new subarray with the new letter
subarrays.append(currentSubarray)
currentSubarray = [array[i]]
currentLetter = array[i][0]
// add the last subarray to the list
subarrays.append(currentSubarray)
return subarrays
function mergeArrays(arrays)
// merge the subarrays back together in alphabetical order
sortedArray = []
while len(arrays) > 0 do
// find the subarray with the first element
// and remove it from the list of subarrays
minIndex = findMinIndex(arrays)
currentSubarray = arrays.pop(minIndex)
// add all the elements in the subarray to the sorted list
for i = 0 to length(currentSubarray)-1 do
sortedArray.append(currentSubarray[i])
return sortedArray
function findMinIndex(arrays)
// find the subarray with the smallest first element
minIndex = 0
for i = 1 to length(arrays)-1 do
if arrays[i][0] < arrays[minIndex][0] then
minIndex = i
return minIndex
- The `librarySort` function takes an array as input and applies the Library Sort algorithm to it. It first divides the array into smaller subarrays based on the first letter of each element. Then, it sorts each subarray independently and merges them back together in alphabetical order.
- The `divideArray` function takes an array as input and divides it into subarrays based on the first letter of each element. It returns a list of subarrays.
- The `mergeArrays` function takes a list of sorted subarrays as input and merges them back together in alphabetical order to create a single sorted array.
- The `findMinIndex` function takes a list of subarrays as input and returns the index of the subarray with the smallest first element.
Overall, the pseudocode implements the Library Sort algorithm by breaking the input array into smaller subarrays, sorting them independently, and then merging them back together.
- The `divideArray` function takes an array as input and divides it into subarrays based on the first letter of each element. It returns a list of subarrays.
- The `mergeArrays` function takes a list of sorted subarrays as input and merges them back together in alphabetical order to create a single sorted array.
- The `findMinIndex` function takes a list of subarrays as input and returns the index of the subarray with the smallest first element.
Overall, the pseudocode implements the Library Sort algorithm by breaking the input array into smaller subarrays, sorting them independently, and then merging them back together.
Sample Code
// C++ code snippet
// function to divide an array of strings into subarrays
vector> divideArray(vector array) {
vector> subarrays;
vector currentSubarray;
char currentLetter = array[0][0];
for (int i = 0; i < array.size(); i++) {
if (array[i][0] == currentLetter) {
currentSubarray.push_back(array[i]);
} else {
subarrays.push_back(currentSubarray);
currentSubarray.clear();
currentSubarray.push_back(array[i]);
currentLetter = array[i][0];
}
}
subarrays.push_back(currentSubarray);
return subarrays;
}
// function to merge sorted subarrays into a single sorted array
vector mergeArrays(vector> arrays) {
vector sortedArray;
while (arrays.size() > 0) {
int minIndex = 0;
for (int i = 1; i < arrays.size(); i++) {
if (arrays[i][0] < arrays[minIndex][0]) {
minIndex = i;
}
}
sortedArray.insert(sortedArray.end(), arrays[minIndex].begin(), arrays[minIndex].end());
arrays.erase(arrays.begin() + minIndex);
}
return sortedArray;
}
// function to apply the Library Sort algorithm to an array of strings
vector librarySort(vector array) {
vector> subarrays = divideArray(array);
for (int i = 0; i < subarrays.size(); i++) {
sort(subarrays[i].begin(), subarrays[i].end());
}
return mergeArrays(subarrays);
}
// main function to test the Library Sort algorithm
int main() {
vector unsortedArray = {"banana", "apple", "pear", "orange", "kiwi", "grape", "strawberry", "pineapple"};
vector sortedArray = librarySort(unsortedArray);
for (int i = 0; i < sortedArray.size(); i++) {
cout << sortedArray[i] << " ";
}
cout << endl;
return 0;
}
# Python code snippet
def divide_array(array):
subarrays = []
current_subarray = []
current_letter = array[0][0]
for element in array:
if element[0] == current_letter:
current_subarray.append(element)
else:
subarrays.append(current_subarray)
current_subarray = [element]
current_letter = element[0]
subarrays.append(current_subarray)
return subarrays
def merge_arrays(arrays):
sorted_array = []
while len(arrays) > 0:
min_index = 0
for i in range(1, len(arrays)):
if arrays[i][0] < arrays[min_index][0]:
min_index = i
sorted_array.extend(arrays[min_index])
arrays.pop(min_index)
return sorted_array
def library_sort(array):
subarrays = divide_array(array)
for i in range(len(subarrays)):
subarrays[i] = sorted(subarrays[i])
return merge_arrays(subarrays)
# test the library_sort function
unsorted_array = ["banana", "apple", "pear", "orange", "kiwi", "grape", "strawberry", "pineapple"]
sorted_array = library_sort(unsorted_array)
print(sorted_array)
import java.util.ArrayList;
import java.util.Collections;
public class LibrarySort {
public static ArrayList> divideArray(ArrayList array) {
ArrayList> subarrays = new ArrayList>();
ArrayList currentSubarray = new ArrayList();
char currentLetter = array.get(0).charAt(0);
for (int i = 0; i < array.size(); i++) {
if (array.get(i).charAt(0) == currentLetter) {
currentSubarray.add(array.get(i));
} else {
subarrays.add(currentSubarray);
currentSubarray = new ArrayList();
currentSubarray.add(array.get(i));
currentLetter = array.get(i).charAt(0);
}
}
subarrays.add(currentSubarray);
return subarrays;
}
public static ArrayList mergeArrays(ArrayList> arrays) {
ArrayList sortedArray = new ArrayList();
while (arrays.size() > 0) {
int minIndex = 0;
for (int i = 1; i < arrays.size(); i++) {
if (arrays.get(i).get(0).compareTo(arrays.get(minIndex).get(0)) < 0) {
minIndex = i;
}
}
sortedArray.addAll(arrays.get(minIndex));
arrays.remove(minIndex);
}
return sortedArray;
}
public static ArrayList librarySort(ArrayList array) {
ArrayList> subarrays = divideArray(array);
for (int i = 0; i < subarrays.size(); i++) {
Collections.sort(subarrays.get(i));
}
return mergeArrays(subarrays);
}
public static void main(String[] args) {
ArrayList unsortedArray = new ArrayList();
unsortedArray.add("banana");
unsortedArray.add("apple");
unsortedArray.add("pear");
unsortedArray.add("orange");
unsortedArray.add("kiwi");
unsortedArray.add("grape");
unsortedArray.add("strawberry");
unsortedArray.add("pineapple");
ArrayList sortedArray = librarySort(unsortedArray);
System.out.println(sortedArray);
}
}