Algorithms

Search

Linear

Binary

Sort

Selection

Bubble

Insertion

String Manipulation

Extract

Delete

Insert

< Back

Components

Version 2.06.26

The "Algorithms" Software Solution Package is a educational and interactive tool that guides the user through the steps of basic algorithms.
The Developer is Andy Tran, and all rights to the application's source code is reserved to the Developer.

Credits

The following people have contributed to the development and maintanence of this project:

Philippe Lhoste - Bugfixing, testing, suggesting new features!

Licenses

The Software Solution Package uses Open-Source Components that are covered by their respective licenses. These Open-Source Components are used with proper rights through the inclusion of attributions, as covered below.

Apache Attributions

  • Copyright 2014 Google
 Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

MIT Attribution

  • Copyright (c) 2011-2014 Twitter, Inc
  • Copyright (c) 2014 Dave Gandy
  • Copyright (c) 2012 Afshin Mehrabani (afshin.meh@gmail.com)
  • Copyright (c) 2013 Brett Camper
 Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

< Back

Linear Search

Dart
int linearSearch(itemToFind, item) {

// Set count and numFound to zero
var count = 0;
var numFound = 0;

// Loop count (the index) to item's length
while (count < item.length) {

// See if item at the index is equal to the input string to be matched
if (item[count] == itemToFind) {

// Add a number to the amount of matches
numFound++;
}

// Increment the index
count++;
}

// Return the amount of matches found
return numFound;
}
PseudoCode
BEGIN linearSearch(itemToFind, item)

// SET count and numFound TO zero
SET count TO 0
SET numFound TO 0

// Loop count (the index) TO item's length
WHILE more items in item

// See if item at the index is equal TO the input string TO be matched
IF (item(count) = itemToFind) THEN

// ADD a number TO the amount of matches
ADD 1 TO numFound
ENDIF

// Increment the index
ADD 1 TO count
ENDWHILE

// Return the amount of matches found
RETURN numFound
END linearSearch


Count
n/a
Matches Found
n/a
Edit the array below, and press on the search button (magnifying glass) above!
Ant
Beetle
Cow
Dog
Frog
Goose
Hen
Moose
Rat
Yak
< Back

Binary Search

Dart
// Note: item MUST be sorted
Int binarySearch(itemToFind, item) {

// Set low, middle as zero
var low = 0;
var middle = 0;

// Set "high" to the last index in item
var high = item.length - 1;

// Set found as false
var found = false;

// If high is larger then or equal to low, or "itemToFind" has not been found, loop
while (high >= low && found == false) {

// The middle is calculated as between low to high
middle = ((low + high).toString() / 2).round();

// If itemToFind is less then item at middle, set the highest to be middle
if (itemToFind < item[middle]) {
high = middle - 1;

// If itemToFind matches item at middle, return a match
} else if (itemToFind == item[middle]) {
found = true;

// Else, set the lowest to be middle (similar to setting highest to be middle)
} else {
low = middle + 1;
}
}

// Return the appropiate message depending on the Binary Search's "found" status
if (found == true) {
return "found!";
} else {
return "Not found!";
}
}
PseudoCode
// Note: item MUST be sorted
BEGIN binarySearch

// Set low, middle as one
SET low TO 1

// Set "high" to the last index in item
SET high TO number of items in list

// Set found as false
SET found TO False

// Get itemToFind
GET itemToFind

// If high is larger then or equal to low, or "itemToFind" has not been found, loop
WHILE high >=low AND found = False

// The middle is calculated as between low to high
SET middle TO INT((low +high)/2)

// If itemToFind is less then item at middle, set the highest to be middle
IF itemToFind < item(middle) THEN
SET high TO middle - 1

// If itemToFind matches item at middle, return a match
ELSEIF itemToFind = item(middle) THEN
SET found TO True

// Else, set the lowest to be middle (similar to setting highest to be middle)
ELSE
SET low TO middle + 1
ENDIF
END WHILE

// Return the appropiate message depending on the Binary Search's "found" status
IF found = True THEN
DISPLAY "found"
ELSE
DISPLAY "Not found"
ENDIF
END binarySearch

Low
n/a
Middle
n/a
High
n/a
Found
n/a
Edit the array below, and press on the search button (magnifying glass) above!
Ant
Beetle
Cow
Dog
Frog
Goose
Hen
Moose
Rat
Yak
< Back

Selection Sort

Dart
List selectionSort(item) {

// Set pass to zero
var pass = 0;

// If the item array has not been fully passed, loop
while (pass < item.length) {

// Set count to be pass plus one
var count = pass + 1;

// Set the minimum to be pass
var minimum = pass;

// Loop again to find the minimum value
while (count < item.length) {
if (item[count] < item[minimum]) {
minimum = count;
}

// Increment count
count = count + 1;
}

// Swap item at the pass with item at the minimum value's index
var temp = item[minimum];
item[minimum] = item[pass];
item[pass] = temp;

// Increment pass
pass++;
}

// Return the sorted item array
return item;
}
PseudoCode
BEGIN selectionSort

// Set pass to one
pass = 1

// If the item array has not been fully passed, loop
WHILE pass < number of items

// Set count to be pass plus one
count = pass + 1

// Set the minimum to be pass
minimum = pass

// Loop again to find the minimum value
WHILE count <= number of items
IF item(count) < item(minimum) THEN
minimum = count
ENDIF

// Increment count
count = count + 1
ENDWHILE

// Swap item at the pass with item at the minimum value's index
SWAP item(minimum) WITH item(pass)

// Increment pass
pass = pass + 1
ENDWHILE

END selectionSort


Minimum
n/a
Pass
n/a
Count
n/a
Edit the array below, and press on the sort button (magnifying glass) above!
Yak
Ant
Beetle
Moose
Cow
Dog
Frog
Hen
Goose
Rat
< Back

Insertion Sort

Dart
List insertionSort(item) {

// Set numItems to be the Length of item
var numItems = item.length - 1;

// Set currentItem (index) to zero
var currentItem = 0;

// Loop until the the item array has been fully processed
while (currentItem <= numItems) {

// Set currentDataitem to be item at the index
var currentDataitem = item[currentItem];

// Set comparison to be zero
var comparison = 0;

// Set finish to be false
var finish = false;

// Loop until sort completed or when the current pass has finished
while (comparison < currentItem && finish == false) {

// If the value of currentDataitem is less then item at comparison, move item at currentItem to the left
if (currentDataitem < item[comparison]) {
var shuffleItem = currentItem;
while (shuffleItem > comparison) {
item[shuffleItem] = item[shuffleItem - 1];
shuffleItem = shuffleItem - 1;
}
item[comparison] = currentDataitem;
finish = true;
}

// Increment comparison
comparison++;
}

// Increment currentItem
currentItem++;
}

// Return the sorted item array
return item;
}
PseudoCode
BEGIN insertionSort

// SET numItems TO be the Length of item
numItems = number of items in list

// SET currentItem (index) TO Two
currentItem = 2

// Loop until the the item array has been fully processed
WHILE currentItem <= numItems

// SET currentDataItem TO be item at the index
SET currentDataItem = item(currentItem)

// SET comparison TO be one
SET comparison = 1

// SET finish TO be false
SET finish = False

// Loop until sort completed or when the current pass has finished
WHILE comparison < currentItem AND finish = False

// If the value of currentDataItem is less then item at comparison, move item at currentItem TO the left
IF currentDataItem < item(comparison) THEN
shuffleItem = currentItem
WHILE shuffleItem > comparison
item(shuffleItem) = item(shuffleItem - 1)
SUBTRACT 1 From shuffleItem
ENDWHILE
item(comparison) = currentDataItem
finish = True
ENDIF

// Increment comparison
ADD 1 TO comparison
ENDWHILE

// Increment currentItem
ADD 1 TO currentItem
ENDWHILE

END insertionSort


Current Item
n/a
Number of Items
n/a
Current Data Item
n/a
Comparison
n/a
Finish
n/a
Shuffle Item
n/a
Temp Shuffle
n/a
Edit the array below, and press on the sort button (magnifying glass) above!
Yak
Ant
Beetle
Frog
Cow
Dog
Hen
Moose
Goose
Rat
< Back

Bubble Sort

Dart
List bubbleSort(item) {

// SET swapped TO be True
var swapped = true;

// SET pass TO be zero
var pass = 0;

// While the array is still swapped, loop
while (swapped) {

// SET Swapped TO be false
swapped = false;

// SET comparison TO be zero
var comparison = 0;

// Loop comparison until the pass is complete
while (comparison < item.length - pass-1) {

// If comparison is less then the number TO the right, swap the values
if (item[comparison] < item[comparison + 1]) {
var temp = item[comparison];
item[comparison] = item[comparison + 1];
item[comparison + 1] = temp;
swapped = true;
}

// Increment comparison
comparison++;
}

// Increment pass
pass++;
}

// Return the sorted item array
return item;
}
PseudoCode
BEGIN bubbleSort

// SET Swapped TO be True
SET swapped = True

// SET pass TO be zero
SET pass = 0

// While the array is still swapped, loop
WHILE swapped = True

// SET Swapped TO be false
swapped = false

// SET comparison TO be one
SET comparison = 1

// Loop comparison until the pass is complete
WHILE comparison < number of items - pass

// If comparison is less then the number TO the right, swap the values
IF item(comparison) < item(comparison + 1)
SWAP item(comparison) with item(comparison + 1)
swapped = True
ENDIF

// Increment comparison
ADD 1 TO comparison
ENDWHILE

// Increment pass
ADD 1 TO pass
ENDWHILE

END bubbleSort


Swapped?
n/a
Pass
n/a
Current Comparison
n/a
Edit the array below, and press on the sort button (magnifying glass) above!
Ant
Frog
Rat
Goose
Beetle
Cow
Dog
Hen
Moose
Yak
< Back

Extract String

Dart
String extractString(start, finish, String extractString) {
var temp = "";
var position = start;

// ADD TO temp, the strings from start TO finish
while (position < finish) {
temp = temp + extractString.split("")[position];
position++;
}

// Return the extracted string
RETURN temp;
}
PseudoCode
BEGIN extract(start,finish,extractString)
temp = Null string
position = start

// ADD TO temp, the strings from start TO finish
WHILE position <= finish
temp = temp + extractString(position)
ADD 1 TO position
ENDWHILE

// Return the extracted string
RETURN temp

END extract

from to

Position
n/a
Start
n/a
Finish
n/a
Output: N/A
< Back

Delete String

Dart
String deleteString(start, finish, String deleteStrings) {
List temp = deleteStrings.split("");
var position = start;
var length = finish - start + 1;
var strlength = deleteStrings.length - length;

// Move variables from temp TO the left
while (position < strlength) {
temp[position] = temp[position + length];
position++;
}

// Use language hacks TO return the deleted (and pruned) string
Iterable range = temp.getRange(0, temp.length-length);
return range.join("");
}
PseudoCode
BEGIN Delete(start,finish,deleteString)
temp = deleteString
position = start
length = finish - start + 1
strlength = length of deleteString - length

// Move variables from temp TO the left
WHILE position <= strlength
temp(position) = temp(position+length)
ADD 1 TO position
ENDWHILE

// In PseudoCode, you can just do this. No joke.
REDUCE size of temp BY length
RETURN temp

END Delete

from to

Start
n/a
Finish
n/a
Position
n/a
Length
n/a
String Length
n/a
Output: N/A
< Back

Insert String

Dart
String insertString(start, String insertStrings, String mainString) {

// Extract starting String
var temp = extractString(0, start, mainString);

// Actually Insert String
temp = temp + insertStrings;
var length = mainString.length;

// Extract Ending String
temp = temp + extractString(start, length, mainString);

// Return the inserted string
RETURN temp;
}
PseudoCode
BEGIN insert(start,insertString,mainString)

// Extract starting String
temp = extract(1,start-1,mainString)

// Actually perform the task of inserting the string
temp = temp + insertString
length = length of mainString

// Extract Ending String
temp = temp + extract(start,length mainString)

// Return the inserted string
RETURN temp

END insert

at

Main String
n/a
Insert String
n/a
Start
n/a
Finish
n/a
Position
n/a
Temp
n/a
Output: N/A