Skip to content

Commit 47e5a47

Browse files
committed
Insertion sort code added
1 parent be3520a commit 47e5a47

File tree

7 files changed

+95
-0
lines changed

7 files changed

+95
-0
lines changed

Sorting/Bubble Sort/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,20 @@ It is called Bubble sort, because *with each iteration the smaller element in th
66

77
Sorting takes place by stepping through all the data items one-by-one in pairs and comparing adjacent data items and swapping each pair that is out of order.
88
![bubble sort demonstration](bubble-sort.png)
9+
10+
11+
---
12+
**A visualization on the algorithm:**
13+
14+
Starting from the beginning of the list, compare every adjacent pair, swap their position if they are not in the right order (the latter one is smaller than the former one). After each iteration, one less element (the last one) is needed to be compared until there are no more elements left to be compared.
15+
16+
![Bubble sort gif](https://upload.wikimedia.org/wikipedia/commons/c/c8/Bubble-sort-example-300px.gif)
17+
18+
####Complexity Analysis
19+
- Worst Case - O(n<sup>2</sup>)
20+
- Average Case - O(n<sup>2</sup>)
21+
- Best Case - O(n)
22+
### More on this topic
23+
- http://en.wikipedia.org/wiki/Bubble_sort
24+
- http://quiz.geeksforgeeks.org/bubble-sort/
25+
- https://www.topcoder.com/community/data-science/data-science-tutorials/sorting/

Sorting/Insertion Sort/Insertion.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class Insertion
2+
{
3+
//Following function will sort the array in Increasing (ascending) order
4+
void sort(int arr[])
5+
{
6+
int i, current_value, j;
7+
8+
int n = arr.length;
9+
10+
for (i = 1; i < n; i++)
11+
{
12+
current_value = arr[i];
13+
j = i-1;
14+
15+
/* Move elements of arr[0..i-1], that are
16+
greater than current_value, to one position ahead
17+
of their current position */
18+
while (j >= 0 && arr[j] > current_value)
19+
{
20+
arr[j+1] = arr[j];
21+
j = j-1;
22+
}
23+
arr[j+1] = current_value;
24+
}
25+
}
26+
}

Sorting/Insertion Sort/Main.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class Main
2+
{
3+
// Prints the array
4+
private static void printList(int arr[])
5+
{
6+
int n = arr.length;
7+
for (int i=0; i<n; ++i)
8+
System.out.print(arr[i] + " ");
9+
System.out.println();
10+
}
11+
12+
public static void main(String[] args)
13+
{
14+
Bubble insertion = new Insertion();
15+
int arr[] = {64, 34, 25, 12, 22, 11, 90};
16+
insertion.sort(arr);
17+
System.out.println("Sorted array");
18+
printList(arr);
19+
}
20+
21+
}

Sorting/Insertion Sort/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Insertion Sort
2+
3+
**Insertion sort** does exactly what you would expect: it inserts each element of the array into its proper position, leaving progressively larger stretches of the array sorted. What this means in practice is that the sort iterates down an array, and the part of the array already covered is in order; then, the current element of the array is inserted into the proper position at the head of the array, and the rest of the elements are moved down, using the space just vacated by the element inserted as the final space.
4+
5+
![Insertion Sort](insertion_sort.jpg)
6+
7+
8+
#####A visualization on Insertion Sort
9+
![Insertion sort demonstration](https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif)
10+
11+
12+
####Complexity Analysis
13+
- Worst Case - О(n<sup>2</sup>) comparisons, swaps
14+
- Average Case - О(n<sup>2</sup>) comparisons, swaps
15+
- Best Case - O(n) comparisons, O(1) swaps
16+
### More on this topic
17+
- https://en.wikipedia.org/wiki/Insertion_sort
18+
- http://quiz.geeksforgeeks.org/insertion-sort/
19+
- https://www.topcoder.com/community/data-science/data-science-tutorials/sorting/
48.5 KB
Loading

Sorting/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#Sorting
2+
3+
>A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists; it is also often useful for canonicalizing data and for producing human-readable output. [wikipedia](https://en.wikipedia.org/wiki/Sorting_algorithm)
4+
5+
6+
7+
###Useful Links:
8+
- [Nice animated visualization of sorting algorithms](https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html)
9+
- [Sorting visualization](https://visualgo.net/sorting)
10+
- [betterexplained.com article on Sorting algorithms](https://betterexplained.com/articles/sorting-algorithms/)
11+
- [Topcoder article](https://www.topcoder.com/community/data-science/data-science-tutorials/sorting/)
12+
- [Comparison of Sorting algorithms](https://en.wikipedia.org/wiki/Sorting_algorithm#Comparison_of_algorithms)

Sorting/sorting_algorithms.png

19.4 KB
Loading

0 commit comments

Comments
 (0)