-2

My goal is not to use any Bubble/Section/Insertion/Merge/Quick Sort—nothing built in.

I am trying to figure out how to:

let arr = [2, 8, 7, 3]

for(let i = 0; i < arr.length; i++) {
  //here I want to number use numbers as an index let's say: arr[0] is 2 right? 
  // I want to set number 2 at index 2 - 1
  //arr[1] = at index 8 - 1 
  //arr[2] = at index 7 - 1
  //arr[3] = at index 3 - 1
  //output will be :    arr [2, 3, 7, 8]
  //here index of each num   ^  ^  ^  ^
  //                         1  2  6  7

 
}

here I tried to remove empty items but I lost whole numbers.screenshot

let arr =[1,3,4,5,6]

let copyArr = [...arr]

copyArr[199] = 200
copyArr[149] = 150

console.log(copyArr)

let str = copyArr.toString().replaceAll(',','').split('')
console.log(str,)


// OUTPUT BELOW:


//copyArr:
// [ 1, 3, 4, 5, 6, <144 empty items>, 150, <49 empty items>, 200 ]

//str :
// [
//   '1', '3', '4', '5',
//   '6', '1', '5', '0',
//   '2', '0', '0'
// ] 
Dave
  • 3
  • 3
  • Why are you creating all those undefined values in the first place? – Barmar Oct 10 '22 at 22:22
  • Your question doesn't seem to have anything to do with sorting. – Barmar Oct 10 '22 at 22:23
  • This question doesn't really make sense. Where are you doing the sorting? You're assigning indexes in the result after you have reordered them, but I don't understand why you're subtracting 1. Why can't you put `2` in index `2` instead of `1`. Also, what will you do if there are duplicate numbers in the array? They can't both go in the same index. – Barmar Oct 10 '22 at 22:31
  • I'm trying to swap the index. as I said I don't want to use any built-in methods. Idk which part confused you all of them are an example. if we have numbers = [25, 17, 38, 98, 134, 5,] and I want to sort it WITHOUT ANY METHODS. if I set the last index of the array in this case number 5 which sits at index 6 and simply set it to 5 - 1 it will jump on at its original index which is index 4. if we do this to all these numbers whole array will be sorted. – Dave Oct 10 '22 at 22:48
  • What will you do if you have `numbers = [1, 2, 1]`? – Barmar Oct 10 '22 at 22:50
  • I still don't understand why you need to subtract 1. Can't you do `result[25] = 25` instead of `result[24] = 25`? It will still be in the correct order. – Barmar Oct 10 '22 at 22:52
  • let's figure first when there is Ascending order no repetitive numbers. – Dave Oct 10 '22 at 22:52
  • because the index starts at ---->0<------ – Dave Oct 10 '22 at 22:52

2 Answers2

0

There's no need to subtract 1. Just assign each value to the corresponding index of the result array. This will work as long as all the values are non-negative integers and there are no duplicates. You don't have to ensure that the starting index is 0, because the step that removes all the empty values will take care of that.

After this, you can use a simple for loop to splice out all the undefined elements that were created in the gaps.

let result = [];
let arr = [2, 8, 7, 3];

for (let i = 0; i < arr.length; i++) {
  result[arr[i]] = arr[i];
}

// remove all the empty elements
for (let i = result.length-1; i >= 0; i--) {
  if (result[i] === undefined) {
    result.splice(i, 1);
  }
}

console.log(result);

See Looping through array and removing items, without breaking for loop for why the second loop counts down instead of up.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • That's what I'm talking about. you did a great job! what do you think about this approach? what will be big o for this? – Dave Oct 12 '22 at 17:12
  • `splice()` is O(n) because it has to shift all the following elements down, so it's O(n^2) – Barmar Oct 12 '22 at 17:13
0

For your sorting question:

let arr = [2, 8, 7, 3]

// from: https://www.geeksforgeeks.org/bubble-sort-algorithms-by-using-javascript/
// Creating the bblSort function
function bblSort(arr) {

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

    // Last i elements are already in place 
    for (var j = 0; j < (arr.length - i - 1); j++) {

      // Checking if the item at present iteration
      // is greater than the next iteration
      if (arr[j] > arr[j + 1]) {

        // If the condition is true then swap them
        var temp = arr[j]
        arr[j] = arr[j + 1]
        arr[j + 1] = temp
      }
    }
  }
  
  return arr;
}

console.log(bblSort(arr).map(item => item - 1))
IT goldman
  • 14,885
  • 2
  • 14
  • 28