Upload files to "arrays"
This commit is contained in:
42
arrays/Example6.js
Normal file
42
arrays/Example6.js
Normal file
@@ -0,0 +1,42 @@
|
||||
|
||||
|
||||
// this function returns the location of the max number stored in array list
|
||||
let getLocOfMax = function(list)
|
||||
{
|
||||
let loc=0
|
||||
let max = list[0]
|
||||
for(let i in list)
|
||||
{
|
||||
if(max<list[i])
|
||||
{
|
||||
max = list[i]
|
||||
loc = i
|
||||
}
|
||||
}
|
||||
return loc;
|
||||
}
|
||||
|
||||
// this function returns the location of the min number stored in array list
|
||||
let getLocOfMin = function(list)
|
||||
{
|
||||
let loc=0
|
||||
let min = list[0]
|
||||
for(let i in list)
|
||||
{
|
||||
if(list[i]<min)
|
||||
{
|
||||
min = list[i]
|
||||
loc = i
|
||||
}
|
||||
}
|
||||
return loc;
|
||||
}
|
||||
|
||||
|
||||
let numbers = [2, 6, 4, 7, 3, 6]
|
||||
|
||||
let minLoc = getLocOfMax(numbers)
|
||||
let maxLoc = getLocOfMin(numbers)
|
||||
console.log("max Loc: " +minLoc)
|
||||
console.log("Min Loc: " +maxLoc)
|
||||
|
||||
82
arrays/ListOpsExample.js
Normal file
82
arrays/ListOpsExample.js
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
In this list example, I perform the foillowing operations
|
||||
on a list of numbers:
|
||||
=> Create the list
|
||||
=> display the list
|
||||
=> search for a number in the list using LINEAR Searching
|
||||
=> Sort the list
|
||||
*/
|
||||
|
||||
|
||||
// function displayList builds a string named s containing all the items from list list
|
||||
// and it displays the s
|
||||
function displayList(list)
|
||||
{
|
||||
let s = list[0] +", " +list[1] +", " +list[2] +", " +list[3] +", " +list[4]
|
||||
+", " +list[5]+", " +list[6] +", " +list[7]+", " +list[8]+", " +list[9]
|
||||
console.log(s)
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Searching algorithm (how to search for a given item in an existing list?)
|
||||
// The following function named searchItem search for a list n in list numList.
|
||||
// If n is found the function returns its location in the list, but if n is not
|
||||
// contained in the list, searchItem return -1
|
||||
function searchItem(list, n)
|
||||
{
|
||||
for(let loc=0; loc<list.length; loc++)
|
||||
{
|
||||
if(list[loc]==n)
|
||||
return loc;
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
|
||||
// This function sort list numList using the selection algorithm
|
||||
function sortList(list)
|
||||
{
|
||||
for (let i = 0; i<list.length; i++)
|
||||
{
|
||||
let loc = i;
|
||||
for (let j = i + 1; j<list.length; j++)
|
||||
{
|
||||
if (list[j] < list[loc])
|
||||
loc = j;
|
||||
}
|
||||
if (loc !== i)
|
||||
{
|
||||
let temp = list[i]
|
||||
list[i] = list[loc]
|
||||
list[loc] = temp
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//The following statement creates a list of 10 integers named numList
|
||||
let numList = [5, 10, -6, 4, 3, 2, -7, 20, 8, 9]
|
||||
|
||||
console.log("Original List")
|
||||
displayList(numList)
|
||||
console.log()
|
||||
|
||||
let size = numList.length; //gets number of items in numList
|
||||
console.log("Items in List: "+ size)
|
||||
console.log()
|
||||
|
||||
let number = 20
|
||||
let loc = searchItem(numList, number) //calls function search list
|
||||
if(loc>=0)
|
||||
console.log(number +" is at pos "+ loc)
|
||||
else
|
||||
console.log(number +" is not in the list")
|
||||
|
||||
console.log()
|
||||
|
||||
sortList(numList)
|
||||
console.log("Sorted List")
|
||||
displayList(numList)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user