0

This is my code searching for an integer in a user-generated array. I can't figure out how to get the code to actually search the array for the code instead of simply stating the integer searched. Any help would be appreciated!

var count = "";
var arr;

function myFunction() {
  var count = prompt("Input array size:");
  document.getElementById("demo_a").innerHTML = "Total Count= " + count;
  arr = [count];
  for (i = 0; i < count; i++) {
    arr[i] = prompt("Enter an integer:");
  }
  document.getElementById("demo_b").innerHTML = arr.toString();
  bubbleSort(arr);
  document.getElementById("demo_b").innerHTML = arr.toString();
}

function myPrompt() {
  var search = prompt("Please enter an integer to search for:");
  document.getElementById("demo_c").innerHTML = "Search For: " + search;
  binarySearch(arr, search);
}

function bubbleSort(arr) {
  var i, j, temp = 0;
  for (i = 0; i < arr.length - 1; i++) {
    for (j = 0; j < arr.length - 1; j++) {
      if (arr[j] > arr[j + 1]) {
        temp = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = temp;
      }
    }
  }
}
<button type="button" onclick="myFunction()">Input data</button>
<button type="button" onclick="myPrompt()">Find Integer</button>
<p id="demo_a"></p>
<p id="demo_b"></p>
<p id="demo_c"></p>
<p id="demo_d"></p>
j08691
  • 204,283
  • 31
  • 260
  • 272
  • 4
    Maybe I'm missing something, but I don't see a definition of `binarySearch`... I believe your array values are going to be strings, by the way, since `prompt()` returns a string. – Heretic Monkey Feb 24 '17 at 19:58
  • 2
    For what it's worth, if you're only bubblesorting (`O(n^2)`) your array so that you can later binary search (`O(logn)`) it, you'd be a lot better off just doing a linear search of the unsorted array (`O(n)`). – CollinD Feb 24 '17 at 20:02
  • Where is `binarySearch`? – ibrahim mahrir Feb 24 '17 at 20:02
  • See http://stackoverflow.com/questions/10264239/fastest-way-to-determine-if-an-element-is-in-a-sorted-array – trincot Feb 24 '17 at 20:02

2 Answers2

0

I am assuming the array is having only integers.

The global variables are removed and creating array as

var arr = new Array(count);

Then, I'm searching using array indexOf method.

function myPrompt(arr) {
   var search = prompt("Please enter an integer to search for:");
   document.getElementById("demo_c").innerHTML = "Search For: " + search;
   if(arr.indexOf(search) > - 1)
   { 
     alert('element Found');
   }
}
RJ-
  • 136
  • 1
  • 13
0

If this is just for practice or learning then check the below answer. Otherwise, you have to think of other techniques like not using alert for input etc.; for small data set javascript is fine to sort and search and also do consider what @CollinD has said.

Anyways, check below code:

var count = '';
var arr;

function myFunction() {
  var count = prompt("Input array size:");
  document.getElementById("demo_a").innerHTML = "Total Count= " + count;
  arr = [count];
  for (i = 0; i < count; i++) {
    arr[i] = prompt("Enter an integer:");
  }
  document.getElementById("demo_b").innerHTML = arr.toString();
  bubbleSort(arr);
  document.getElementById("demo_b").innerHTML = arr.toString();
}

function myPrompt() {
  var search = prompt("Please enter an integer to search for:");
  document.getElementById("demo_c").innerHTML = "Search For: " + search;
  linearSearch(arr, search);
}

function linearSearch(arr, search) { 

  var found = false; 
  var foundAt; 

  arr.some(function(val, index) {

    if (val == search) {

      foundAt = index;   
      found = true;
      return true;  
    }

  }); 
  if (found) {  
    document.getElementById("demo_d").innerHTML = "Found " + search + ' at index ' + foundAt;  
  } else {   
    document.getElementById("demo_d").innerHTML = "Not Found " + search;   
  }
}

function bubbleSort(arr) {
  var i, j, temp = 0;
  for (i = 0; i < arr.length - 1; i++) {
    for (j = 0; j < arr.length - 1; j++) {
      if (arr[j] > arr[j + 1]) {
        temp = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = temp;
      }
    }
  }
}
<button type="button" onclick="myFunction()">Input data</button>
<button type="button" onclick="myPrompt()">Find Integer</button>
<p id="demo_a"></p>
<p id="demo_b"></p>
<p id="demo_c"></p>
<p id="demo_d"></p>
Gaurav
  • 1,233
  • 14
  • 23