Linear arrays: Difference between revisions

From Computer Science Wiki
(Created page with "right|frame|Linear arrays<ref>http://www.flaticon.com/</ref> == Identifying examples of abstraction == (awaiting permission to post material) '''Anyt...")
 
 
(19 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[file:arrows.png|right|frame|Linear arrays<ref>http://www.flaticon.com/</ref>]]
[[file:arrows.png|right|frame|Linear arrays<ref>http://www.flaticon.com/</ref>]]


There are four common algorithms we use on a linear array. Although the IB asks only for you to know these at the pseudocode level, I suggest you memorize these for the rest of your life.


You will remember we use  [[arrays|arrays]] to hold values of the same type at contiguous memory locations. In particular, the use of arrays allows us to create "groups" or "clusters" of variables without needing to give a unique variable name to each, but still allowing us to individually index into the elements of the array.<ref>http://cs50.wiki/Arrays+and+strings</ref>


== Identifying examples of abstraction ==
== Standard algorithms ==


(awaiting permission to post material)
* Sequential search
* Binary search
* Bubble sort
* Selection sort


'''Anytime you see a simple interface covering a more complex system''', you should think "abstraction".  
=== Sequential search ===
In computer science, linear search or sequential search is a method for finding a target value within a list. It sequentially checks each element of the list for the target value until a match is found or until all the elements have been searched.<ref>https://en.wikipedia.org/wiki/Linear_search</ref>


* A car is a very complex machine but the interface is simple (a steering wheel, a gas pedal and a gear shift)  
=== Binary Search ===
* A video game controller only has a few buttons, but underneath the controller is complex control mechanism
In computer science, binary search, also known as half-interval search, is a search algorithm that finds the position of a target value within a '''sorted''' array (''emphasis mine''). Binary search compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful. If the search ends with the remaining half being empty, the target is not in the array.<ref>https://en.wikipedia.org/wiki/Binary_search_algorithm</ref>
* A programming language can be fairly simple, but it translates the instructions you write into machine code, which is impossibly complex


== Explain why abstraction is required ==
* [https://www.cs.usfca.edu/~galles/visualization/Search.html Click here for a good visualization of binary search]


You must decide '''at what level should I abstract a problem and solution'''. Abstraction is required because life is complex. We need to simplify complex systems so people can understand and use them.  
=== Bubble sort ===
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. <ref>https://en.wikipedia.org/wiki/Bubble_sort</ref>


For example, a student is building a software program to help students pick the right college. The student uses SAT scores to determine a good match. The student has '''abstracted''' complexity when it comes to choosing a college. There are actually many factors in choosing a college, but the student has chosen to focus on SAT scores.
* [https://visualgo.net/bn/sorting This is a good animation which not only shows bubble sorts, but also shows the code stepping through a sort]


== Construct an abstraction ==
<syntaxhighlight lang=python>
# Thank you to Alex for submitting this code!!!


I want to launch a missile, loaded with 10,000 liters of peanut butter, at my friend because it would be funny. There are hundreds of important variables associated with launching, flying, and hitting a target in this example. Please think for a moment how we could construct an abstraction.
def bubbleSort(list):
    swapped = True
    index_of_last_element = len(list)-1
    while swapped:
        swapped = False
        for i in range(0,index_of_last_element):   
            if list[i] > list[(i+1)]:
                firstNumber = list[i]
                secondNumber = list[(i+1)]
                list[i] = secondNumber
                list[(i+1)] = firstNumber
                swapped=True
        index_of_last_element -= 1
    return list
</syntaxhighlight>


We could create a giant red button with the words "launch peanut butter missile at friend" which would launch the missile. However once this button was pushed a VERY COMPLEX process would begin to launch the missile. In this example, the button is an abstraction of a missile launch.
=== Selection Sort ===
In computer science, selection sort is a sorting algorithm, specifically an in-place comparison sort. The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.<ref>https://en.wikipedia.org/wiki/Selection_sort</ref>
<br />
I'm grateful to wikipedia for the code below<ref>https://en.wikipedia.org/wiki/Selection_sort</ref>
<br />
<syntaxhighlight lang="python">
Sorted sublist == ( )
Unsorted sublist == (11, 25, 12, 22, 64)
Lowest element in unsorted list == 11


== Distinguish between a real-world entity and its abstraction ==
Sorted sublist == (11)
Unsorted sublist == (25, 12, 22, 64)
Lowest element in unsorted list == 12


Please refer to the missile example above.
Sorted sublist == (11, 12)
Unsorted sublist == (25, 22, 64)
Lowest element in unsorted list == 22
 
Sorted sublist == (11, 12, 22)
Unsorted sublist == (25, 64)
Lowest element in unsorted list == 25
 
Sorted sublist == (11, 12, 22, 25)
Unsorted sublist == (64)
Lowest element in unsorted list == 64
 
Sorted sublist == (11, 12, 22, 25, 64)
Unsorted sublist == ( )
</syntaxhighlight>


== Do you understand this? ==
== Do you understand this? ==
[https://www.toptal.com/developers/sorting-algorithms This site is truly excellent resource to help you visually better understand sorting]


== Standards ==  
== Standards ==  

Latest revision as of 09:05, 13 April 2022

Linear arrays[1]

There are four common algorithms we use on a linear array. Although the IB asks only for you to know these at the pseudocode level, I suggest you memorize these for the rest of your life.

You will remember we use arrays to hold values of the same type at contiguous memory locations. In particular, the use of arrays allows us to create "groups" or "clusters" of variables without needing to give a unique variable name to each, but still allowing us to individually index into the elements of the array.[2]

Standard algorithms[edit]

  • Sequential search
  • Binary search
  • Bubble sort
  • Selection sort

Sequential search[edit]

In computer science, linear search or sequential search is a method for finding a target value within a list. It sequentially checks each element of the list for the target value until a match is found or until all the elements have been searched.[3]

Binary Search[edit]

In computer science, binary search, also known as half-interval search, is a search algorithm that finds the position of a target value within a sorted array (emphasis mine). Binary search compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful. If the search ends with the remaining half being empty, the target is not in the array.[4]

Bubble sort[edit]

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. [5]

# Thank you to Alex for submitting this code!!! 

def bubbleSort(list):
    swapped = True
    index_of_last_element = len(list)-1
    while swapped:
        swapped = False 
        for i in range(0,index_of_last_element):    
            if list[i] > list[(i+1)]:
                firstNumber = list[i]
                secondNumber = list[(i+1)]
                list[i] = secondNumber
                list[(i+1)] = firstNumber
                swapped=True
        index_of_last_element -= 1
    return list

Selection Sort[edit]

In computer science, selection sort is a sorting algorithm, specifically an in-place comparison sort. The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.[6]
I'm grateful to wikipedia for the code below[7]

Sorted sublist == ( )
Unsorted sublist == (11, 25, 12, 22, 64)
Lowest element in unsorted list == 11

Sorted sublist ==  (11)
Unsorted sublist == (25, 12, 22, 64)
Lowest element in unsorted list == 12

Sorted sublist == (11, 12)
Unsorted sublist == (25, 22, 64)
Lowest element in unsorted list == 22

Sorted sublist == (11, 12, 22)
Unsorted sublist == (25, 64)
Lowest element in unsorted list == 25

Sorted sublist == (11, 12, 22, 25)
Unsorted sublist == (64)
Lowest element in unsorted list == 64

Sorted sublist == (11, 12, 22, 25, 64)
Unsorted sublist == ( )

Do you understand this?[edit]

This site is truly excellent resource to help you visually better understand sorting

Standards[edit]

These standards are used from the IB Computer Science Subject Guide[8]

  • Describe the characteristics of standard algorithms on linear arrays.

References[edit]