about | help | code help+videos | done | prefs |
tmhscs@gmail.com sort
_____ _ _ _ _ _ _ / ____| | | (_) /\ | | (_) | | | | (___ ___ _ __| |_ _ _ __ __ _ / \ | | __ _ ___ _ __ _| |_| |__ _ __ ___ ___ \___ \ / _ \| '__| __| | '_ \ / _` | / /\ \ | |/ _` |/ _ \| '__| | __| '_ \| '_ ` _ \/ __| ____) | (_) | | | |_| | | | | (_| | / ____ \| | (_| | (_) | | | | |_| | | | | | | | \__ \ |_____/ \___/|_| \__|_|_| |_|\__, | /_/ \_\_|\__, |\___/|_| |_|\__|_| |_|_| |_| |_|___/ __/ | __/ | |___/ |___/Program the following Sorting Algorithms to sort an array of integers from least to greatest.
Bubble Sort is a simple but inefficient sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. You'll need two for-loops and one if-statement to solve Bubble Sort. The first for-loop is which pass number you are on, and the second for-loop is for which element you are currently comparing with the adjacent one. The if-statement checks if they are out of order and swaps them if necessary!
01. sort_bubbleSortOnePass H
02. sort_bubbleSort H
Selection Sort works by repeatedly finding the smallest element and swaps it with the first spot, then finds the next smallest and swaps it with the second spot, and so forth. You'll need two for-loops and one if-statement to solve Selection Sort. The first for-loop is for which spot you are going to swap into, and the second for-loop is for finding the smallest element from that spot to the end. The if-statement checks if you have found a smaller element and updates an integer to remember its index so that after the inner for-loop is done you can make the swap!
03. sort_selectionSortOnePass H
04. sort_selectionSort H
Insertion Sort works by repeatedly swapping an element one spot to the left at a time as long as it is smaller. You'll need two loops to solve Insertion Sort. The first for-loop is for which spot you are going to be swapping towards the left, and the second loop can be a while loop that goes from the current spot towards the start of the array swapping this element with the previous if they are out of order.
05. sort_insertionSortOnePass H
06. sort_insertionSort H
Authoring docs
Copyright Nick Parlante 2017 - privacy