0% found this document useful (0 votes)
138 views12 pages

Informática

The document describes algorithms for swapping elements in an array, finding the minimum value in a subarray, selection sort, and binary search. It includes code implementations of each algorithm and test cases to validate the code works correctly. The code is written in JavaScript and utilizes common array functions like swap to rearrange elements, find minimum to get the lowest value index, selection sort to reorder the array in ascending order, and binary search to efficiently find a target value index.

Uploaded by

Andy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views12 pages

Informática

The document describes algorithms for swapping elements in an array, finding the minimum value in a subarray, selection sort, and binary search. It includes code implementations of each algorithm and test cases to validate the code works correctly. The code is written in JavaScript and utilizes common array functions like swap to rearrange elements, find minimum to get the lowest value index, selection sort to reorder the array in ascending order, and binary search to efficiently find a target value index.

Uploaded by

Andy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Desafío: implementa el intercambio

var swap = function(array, firstIndex, secondIndex) {

var temp = array[firstIndex];

array[firstIndex] = array[secondIndex];

array[secondIndex] = temp;

};

var testArray = [7, 9, 4];

swap(testArray, 0, 1);

println(testArray);

Program.assertEqual(testArray, [9, 7, 4]);

//Probando el algoritmo

swap(testArray, 0, 2);

Program.assertEqual(testArray, [4, 7, 9]);

swap(testArray, 1, 2);

Program.assertEqual(testArray, [4, 9, 7]);


Desafío: encuentra el valor mínimo en un subarreglo
var indexOfMinimum = function(array, startIndex) {

// Set initial values for minValue and minIndex,

// based on the leftmost entry in the subarray:

var minValue = array[startIndex];

var minIndex = startIndex;

// Loop over items starting with startIndex,

// updating minValue and minIndex as needed:

for(var i = minIndex + 1; i < array.length; i++) {

if(array[i] < minValue) {

minIndex = i;

minValue = array[i];

return minIndex;

};

var array = [18, 6, 66, 44, 9, 22, 14];

var index = indexOfMinimum(array, 2);


// For the test array [18, 6, 66, 44, 9, 22, 14],

// the value 9 is the smallest of [..66, 44, 9, 22, 14]

// Since 9 is at index 4 in the original array,

// "index" has value 4

println("The index of the minimum value of the subarray starting at index 2 is " + index + "." );

Program.assertEqual(index, 4);

//Probando el algoritmo

Program.assertEqual(indexOfMinimum(array,7),7);

Program.assertEqual(indexOfMinimum(array,5),6);
Desafío: implementa el ordenamiento por selección
var swap = function(array, firstIndex, secondIndex) {

var temp = array[firstIndex];

array[firstIndex] = array[secondIndex];

array[secondIndex] = temp;

};

var indexOfMinimum = function(array, startIndex) {

var minValue = array[startIndex];

var minIndex = startIndex;

for(var i = minIndex + 1; i < array.length; i++) {

if(array[i] < minValue) {

minIndex = i;

minValue = array[i];

return minIndex;

};

var selectionSort = function(array) {

var i;

var a;
for(i=0;i<array.length ;i++) {

a = indexOfMinimum(array,i);

swap(array,i,a);

};

var array = [22, 11, 99, 88, 9, 7, 42];

selectionSort(array);

println("Array after sorting: " + array);

Program.assertEqual(array, [7, 9, 11, 22, 42, 88, 99]);

//Segunda parte

var array1 = [22, 30, 99, 88, 6, 7, 15];

selectionSort(array1);

Program.assertEqual(array1,[6,7,15,22,30,88,99]);
Desafío: implementa el intercambio
var swap = function(array, firstIndex, secondIndex) {

var temp = array[firstIndex];

array[firstIndex] = array[secondIndex];

array[secondIndex] = temp;

};

var testArray = [7, 9, 4];

swap(testArray, 0, 1);

println(testArray);

Program.assertEqual(testArray, [9, 7, 4]);

//Probando el algoritmo

swap(testArray, 0, 2);

Program.assertEqual(testArray, [4, 7, 9]);

swap(testArray, 1, 2);

Program.assertEqual(testArray, [4, 9, 7]);


Desafío: encuentra el valor mínimo en un subarreglo
var indexOfMinimum = function(array, startIndex) {

// Set initial values for minValue and minIndex,

// based on the leftmost entry in the subarray:

var minValue = array[startIndex];

var minIndex = startIndex;

// Loop over items starting with startIndex,

// updating minValue and minIndex as needed:

for(var i = minIndex + 1; i < array.length; i++) {

if(array[i] < minValue) {

minIndex = i;

minValue = array[i];

return minIndex;

};

var array = [18, 6, 66, 44, 9, 22, 14];

var index = indexOfMinimum(array, 2);


// For the test array [18, 6, 66, 44, 9, 22, 14],

// the value 9 is the smallest of [..66, 44, 9, 22, 14]

// Since 9 is at index 4 in the original array,

// "index" has value 4

println("The index of the minimum value of the subarray starting at index 2 is " + index + "." );

Program.assertEqual(index, 4);

//Probando el algoritmo

Program.assertEqual(indexOfMinimum(array,7),7);

Program.assertEqual(indexOfMinimum(array,5),6);
Desafío: implementa el ordenamiento por selección
var swap = function(array, firstIndex, secondIndex) {

var temp = array[firstIndex];

array[firstIndex] = array[secondIndex];

array[secondIndex] = temp;

};

var indexOfMinimum = function(array, startIndex) {

var minValue = array[startIndex];

var minIndex = startIndex;

for(var i = minIndex + 1; i < array.length; i++) {

if(array[i] < minValue) {

minIndex = i;

minValue = array[i];

return minIndex;

};

var selectionSort = function(array) {

var i;

var a;
for(i=0;i<array.length ;i++) {

a = indexOfMinimum(array,i);

swap(array,i,a);

};

var array = [22, 11, 99, 88, 9, 7, 42];

selectionSort(array);

println("Array after sorting: " + array);

Program.assertEqual(array, [7, 9, 11, 22, 42, 88, 99]);

//Segunda parte

var array1 = [22, 30, 99, 88, 6, 7, 15];

selectionSort(array1);

Program.assertEqual(array1,[6,7,15,22,30,88,99]);
Desafío: búsqueda binaria
/* Returns either the index of the location in the array,

or -1 if the array did not contain the targetValue */

var doSearch = function(array, targetValue){

var min = 0;

var max = array.length - 1;

var guess;

var intentos = 0;

while (max>=min){

//Esto es lo que cuenta los intentos

intentos++;

//promediando

guess = Math.floor((max+min)/2);

//se empieza a iterar

if(array[guess]===targetValue){

println("Lo has encontrado en la posicion" + guess);

println("me tomo" + intentos + "intentos encontrarlo");

return guess;

else if(array[guess]<targetValue){

min=guess+1;

}
else{

max=guess-1;

//Cuando se responde -1 quiere decir que el valor no esta en "primes"

return -1;

};

var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,

41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];

var result = doSearch(primes, 71);

println("Found prime at index " + result);

//Probando el programa

Program.assertEqual(doSearch(primes, 73), 20);

Program.assertEqual(doSearch(primes, 79), 21);

Program.assertEqual(doSearch(primes, 71), 19);

You might also like