Selection Sort
The selection sort algorithm orders array elements by finding the smallest element in the array. This element is exchanged with the first element in the array. Next, the algorithm finds the second smallest element in the array, and exchanges it with the second element. This continues until the array is sorted.
Insertion Sort
The insertion sort algorithm begins at the first element and considers it sorted. Then, the algorithm looks at the second element and compares it to the first. The second element is placed where it needs to be in relation to the first element. Now, the first two elements are sorted. The algorithm will look at the third element and compare it to the two sorted elements and place it where it needs to be. Now, the first three elements are sorted. This process continues until the array is sorted.
Merge Sort
The merge sort algorithm halves the array until the array is broken up into individual elements. The merge sort algorithm does this using recursion (the sort algorithm keeps calling itself). Once the elements are individualized, adjacent elements are merged together and sorted simultaneously. (they're merged back into groups of 2, then those groups of 2 are merged into 4, then 8, and so on). This continues until there is only 1 sorted group with all the elements.
The sorting algorithms here are listed from least efficient to most efficient (selection < insertion < merge), especially when working with large arrays.
Subscribe to:
Post Comments (Atom)
Searching Algorithms
Linear/Sequential Search A linear/sequential search algorithm looks at each element in the array until it finds what it's looking for. ...
-
A recursive function is one that calls itself in the method body. A recursive function breaks down a problem into smaller iterative pieces...
-
Linear/Sequential Search A linear/sequential search algorithm looks at each element in the array until it finds what it's looking for. ...
-
Cohesion - a class is cohesive if it has a single, well-defined purpose. If the class has methods and instance fields that don't exactly...
No comments:
Post a Comment