Skip to content

Commit b39eb28

Browse files
committed
Insertion, Linear Search, Quicksort and Selection Added
Insertion, Linear Search, Quicksort and Selection algorithms added.
1 parent 0479eb5 commit b39eb28

File tree

5 files changed

+165
-0
lines changed

5 files changed

+165
-0
lines changed
File renamed without changes.

Insertionsort/readme.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Insertion Sort
2+
3+
Goal: Sort an array from low to high (or high to low).
4+
5+
You are given an array of numbers and need to put them in the right order. The insertion sort algorithm works as follows:
6+
7+
- Put the numbers on a pile. This pile is unsorted.
8+
- Pick a number from the pile. It doesn't really matter which one you pick, but it's easiest to pick from the top of the pile.
9+
- Insert this number into a new array.
10+
- Pick the next number from the unsorted pile and also insert that into the new array. It either goes before or after the first number you picked, so that now these two numbers are sorted.
11+
- Again, pick the next number from the pile and insert it into the array in the proper sorted position.
12+
- Keep doing this until there are no more numbers on the pile. You end up with an empty pile and an array that is sorted.
13+
14+
That's why this is called an "insertion" sort, because you take a number from the pile and insert it in the array in its proper sorted position.
15+
16+
## An example
17+
18+
Let's say the numbers to sort are `[ 8, 3, 5, 4, 6 ]`. This is our unsorted pile.
19+
20+
Pick the first number, `8`, and insert it into the new array. There is nothing in that array yet, so that's easy. The sorted array is now `[ 8 ]` and the pile is `[ 3, 5, 4, 6 ]`.
21+
22+
Pick the next number from the pile, `3`, and insert it into the sorted array. It should go before the `8`, so the sorted array is now `[ 3, 8 ]` and the pile is reduced to `[ 5, 4, 6 ]`.
23+
24+
Pick the next number from the pile, `5`, and insert it into the sorted array. It goes in between the `3` and `8`. The sorted array is `[ 3, 5, 8 ]` and the pile is `[ 4, 6 ]`.
25+
26+
Repeat this process until the pile is empty.
27+
28+
## In-place sort
29+
30+
The above explanation makes it seem like you need two arrays: one for the unsorted pile and one that contains the numbers in sorted order.
31+
32+
But you can perform the insertion sort *in-place*, without having to create a separate array. You just keep track of which part of the array is sorted already and which part is the unsorted pile.
33+
34+
Initially, the array is `[ 8, 3, 5, 4, 6 ]`. The `|` bar shows where the sorted portion ends and the pile begins:
35+
36+
[| 8, 3, 5, 4, 6 ]
37+
38+
This shows that the sorted portion is empty and the pile starts at `8`.
39+
40+
After processing the first number, we have:
41+
42+
[ 8 | 3, 5, 4, 6 ]
43+
44+
The sorted portion is `[ 8 ]` and the pile is `[ 3, 5, 4, 6 ]`. The `|` bar has shifted one position to the right.
45+
46+
This is how the content of the array changes during the sort:
47+
48+
[| 8, 3, 5, 4, 6 ]
49+
[ 8 | 3, 5, 4, 6 ]
50+
[ 3, 8 | 5, 4, 6 ]
51+
[ 3, 5, 8 | 4, 6 ]
52+
[ 3, 4, 5, 8 | 6 ]
53+
[ 3, 4, 5, 6, 8 |]
54+
55+
In each step, the `|` bar moves up one position. As you can see, the beginning of the array up to the `|` is always sorted. The pile shrinks by one and the sorted portion grows by one, until the pile is empty and there are no more unsorted numbers left.
56+
57+
## How to insert
58+
59+
At each step you pick the top-most number from the unsorted pile and insert it into the sorted portion of the array. You must put that number in the proper place so that the beginning of the array remains sorted. How does that work?
60+
61+
Let's say we've already done the first few elements and the array looks like this:
62+
63+
[ 3, 5, 8 | 4, 6 ]
64+
65+
The next number to sort is `4`. We need to insert that into the sorted portion `[ 3, 5, 8 ]` somewhere.
66+
67+
Here's one way to do this: Look at the previous element, `8`.
68+
69+
[ 3, 5, 8, 4 | 6 ]
70+
^
71+
72+
Is this greater than `4`? Yes it is, so the `4` should come before the `8`. We swap these two numbers to get:
73+
74+
[ 3, 5, 4, 8 | 6 ]
75+
<-->
76+
swapped
77+
78+
We're not done yet. The new previous element, `5`, is also greater than `4`. We also swap these two numbers:
79+
80+
[ 3, 4, 5, 8 | 6 ]
81+
<-->
82+
swapped
83+
84+
Again, look at the previous element. Is `3` greater than `4`? No, it is not. That means we're done with number `4`. The beginning of the array is sorted again.
85+
86+
This was a description of the inner loop of the insertion sort algorithm, which you'll see in the next section. It inserts the number from the top of the pile into the sorted portion by swapping numbers.

Linearsearch/readme.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Linear Search
2+
3+
Goal: Find a particular value in an array.
4+
5+
We have an array of generic objects. With linear search, we iterate over all the objects in the array and compare each one to the object we're looking for. If the two objects are equal, we stop and return the current array index. If not, we continue to look for the next object as long as we have objects in the array.
6+
7+
## An example
8+
9+
Let's say we have an array of numbers `[5, 2, 4, 7]` and we want to check if the array contains the number `2`.
10+
11+
We start by comparing the first number in the array, `5`, to the number we're looking for, `2`. They are obviously not the same, and so we continue to the next array element.
12+
13+
We compare the number `2` from the array to our number `2` and notice they are equal. Now we can stop our iteration and return 1, which is the index of the number `2` in the array.

Quicksort/readme.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Quicksort
2+
3+
Goal: Sort an array from low to high (or high to low).
4+
5+
Quicksort is one of the most famous algorithms in history. It was invented way back in 1959 by Tony Hoare, at a time when recursion was still a fairly nebulous concept.
6+
7+
Here's how it works. When given an array, `quicksort()` splits it up into three parts based on a "pivot" variable. Here, the pivot is taken to be the element in the middle of the array (later on you'll see other ways to choose the pivot).
8+
9+
All the elements less than the pivot go into a new array called `less`. All the elements equal to the pivot go into the `equal` array. And you guessed it, all elements greater than the pivot go into the third array, `greater`. This is why the generic type `T` must be `Comparable`, so we can compare the elements with `<`, `==`, and `>`.
10+
11+
Once we have these three arrays, `quicksort()` recursively sorts the `less` array and the `greater` array, then glues those sorted subarrays back together with the `equal` array to get the final result.

Selectionsort/readme.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Selection Sort
2+
3+
Goal: To sort an array from low to high (or high to low).
4+
5+
You are given an array of numbers and need to put them in the right order. The selection sort algorithm divides the array into two parts: the beginning of the array is sorted, while the rest of the array consists of the numbers that still remain to be sorted.
6+
7+
[ ...sorted numbers... | ...unsorted numbers... ]
8+
9+
This is similar to [insertion sort](../Insertion%20Sort/), but the difference is in how new numbers are added to the sorted portion.
10+
11+
It works as follows:
12+
13+
- Find the lowest number in the array. You must start at index 0, loop through all the numbers in the array, and keep track of what the lowest number is.
14+
- Swap the lowest number with the number at index 0. Now, the sorted portion consists of just the number at index 0.
15+
- Go to index 1.
16+
- Find the lowest number in the rest of the array. This time you start looking from index 1. Again you loop until the end of the array and keep track of the lowest number you come across.
17+
- Swap the lowest number with the number at index 1. Now, the sorted portion contains two numbers and extends from index 0 to index 1.
18+
- Go to index 2.
19+
- Find the lowest number in the rest of the array, starting from index 2, and swap it with the one at index 2. Now, the array is sorted from index 0 to 2; this range contains the three lowest numbers in the array.
20+
- And continue until no numbers remain to be sorted.
21+
22+
It is called a "selection" sort because at every step you search through the rest of the array to select the next lowest number.
23+
24+
## An example
25+
26+
Suppose the numbers to sort are `[ 5, 8, 3, 4, 6 ]`. We also keep track of where the sorted portion of the array ends, denoted by the `|` symbol.
27+
28+
Initially, the sorted portion is empty:
29+
30+
[| 5, 8, 3, 4, 6 ]
31+
32+
Now we find the lowest number in the array. We do that by scanning through the array from left to right, starting at the `|` bar. We find the number `3`.
33+
34+
To put this number into the sorted position, we swap it with the number next to the `|`, which is `5`:
35+
36+
[ 3 | 8, 5, 4, 6 ]
37+
* *
38+
39+
The sorted portion is now `[ 3 ]` and the rest is `[ 8, 5, 4, 6 ]`.
40+
41+
Again, we look for the lowest number, starting from the `|` bar. We find `4` and swap it with `8` to get:
42+
43+
[ 3, 4 | 5, 8, 6 ]
44+
* *
45+
46+
With every step, the `|` bar moves one position to the right. We again look through the rest of the array and find `5` as the lowest number. There is no need to swap `5` with itself, and we simply move forward:
47+
48+
[ 3, 4, 5 | 8, 6 ]
49+
*
50+
51+
This process repeats until the array is sorted. Note that everything to the left of the `|` bar is always in sorted order and always contains the lowest numbers in the array. Finally, we end up with:
52+
53+
[ 3, 4, 5, 6, 8 |]
54+
55+
The selection sort is an *in-place* sort because everything happens in the same array without using additional memory. You can also implement this as a *stable* sort so that identical elements do not get swapped around relative to each other (note that the version given below is not stable).

0 commit comments

Comments
 (0)