这里我们使用排序函数对数组进行排序,并打印数组和新元素在 DOM 中的位置。
方法:
- 创建一个空数组。
- 现在通过将参数作为数组来创建排序函数。
- 现在创建一个 onClick函数,因为只要按下按钮,该函数自动运行。
- 在 onClick函数,我们首先将元素添加到数组中,然后使用迭代器函数找到元素的位置
- 在查找位置时,我们将查看数组元素是否大于或不大于,如果是,则标记该位置并中断循环。
- 现在最后,我们将数组和新添加元素的位置打印到 DOM 中。
HTML
Input the elements in the array :
gfg.js
// Creating an empty array and when we add the
// elements we increase the number of elements
// by 1
const arr = Array();
var numberOfElements = 0;
// Sorting function to sort the array
// Here we use the selection sort
// algorithm for sorting
function sort(arr) {
for (let i = 0; i < arr.length; i++) {
let min = i;
for (let j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[min])
min = j;
}
let temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
// onClick function add
function add() {
// Taking the input from the text box to
// add the element to the given array
arr[numberOfElements] = parseInt(
document.getElementById("element").value);
// Increasing the array length to add
// next number to the array
numberOfElements++;
// Variables to find the position of
// the newly added element
var position = 0;
var flag = false;
// Finding the position of the newly added
// element in the sorted array
var newElement = arr[numberOfElements - 1];
for (let i = 0; i < arr.length; i++) {
// If the array element is greater then
// the newly added element than mark that
// position and break the loop as this is
// the position of newly added element in
// the gieven sorted array
if (arr[i] > newElement) {
position = i;
flag = true;
break;
}
}
// If the newly added element is highest among
// the elements in the array then its position
// would be the array length - 1
if (flag == false)
position = arr.length - 1;
// Now calling the sort function to sort the
// given array after insertion
sort(arr);
// For printing into the DOM
document.getElementById("element").value = "";
var print = "Array is : ";
for (let i = 0; i < arr.length; i++) {
print += arr[i] + " ";
}
document.getElementById("array").innerHTML = print;
document.getElementById("new-position-array").innerHTML
= "New element: " + newElement + " at Position: "
+ position;
}
gfg.js
// Creating an empty array and when we add the
// elements we increase the number of elements
// by 1
const arr = Array();
var numberOfElements = 0;
// Sorting function to sort the array
// Here we use the selection sort
// algorithm for sorting
function sort(arr) {
for (let i = 0; i < arr.length; i++) {
let min = i;
for (let j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[min])
min = j;
}
let temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
// onClick function add
function add() {
// Taking the input from the text box to
// add the element to the given array
arr[numberOfElements] = parseInt(
document.getElementById("element").value);
// Increasing the array length to add
// next number to the array
numberOfElements++;
// Variables to find the position of
// the newly added element
var position = 0;
var flag = false;
// Finding the position of the newly added
// element in the sorted array
var newElement = arr[numberOfElements - 1];
for (let i = 0; i < arr.length; i++) {
// If the array element is greater then
// the newly added element than mark that
// position and break the loop as this is
// the position of newly added element in
// the gieven sorted array
if (arr[i] > newElement) {
position = i;
flag = true;
break;
}
}
// If the newly added element is highest among
// the elements in the array then its position
// would be the array length - 1
if (flag == false)
position = arr.length - 1;
// Now calling the sort function to sort the
// given array after insertion
sort(arr);
// For printing into the DOM
document.getElementById("element").value = "";
var print = "Array is : ";
for (let i = 0; i < arr.length; i++) {
print += arr[i] + " ";
}
document.getElementById("array").innerHTML = print;
document.getElementById("new-position-array").innerHTML
= "New element: " + newElement + " at Position: "
+ position;
}
使用的功能:
sort()函数:此函数接受要排序的所需数组的参数,并实现选择排序,并作为结果为我们提供已排序的数组。
add()函数:这个函数是一个点击函数,当按钮被点击时触发。此函数从输入框中获取值并将其存储到已排序的数组中,并将数组索引值加一。然后,此函数通过将新添加的元素与数组中的每个元素进行比较,找到新添加元素在排序数组中的位置,如果发现值大于它,则中断循环并存储该位置的索引,然后调用sort()函数再次对数组进行排序。最后,它在 DOM 中给出输出
输出: