- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2024 06:06 AM
Hi,
I am trying the below code in Background scripts, but unfinished, can somebody how to execute successfully?
this is sorting array elements: ( I want to achive this without using array methods)
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2024 09:07 PM
Hi @dvass0107
Please use basic bubble sort method,
Ascending Order
var ar = [5, 3, 7, 1, 10];
for (var i = 0; i < ar.length; i++) {
for (var j = 0; j < ar.length - 1; j++) {
if (ar[j] > ar[j + 1]) {
// Swap elements
var temp = ar[j];
ar[j] = ar[j + 1];
ar[j + 1] = temp;
}
}
}
gs.print("Sorted array in ascending order: " + ar);
var ar = [5, 3, 7, 1, 10];
for (var i = 0; i < ar.length; i++) {
for (var j = 0; j < ar.length - 1; j++) {
if (ar[j] > ar[j + 1]) {
// Swap elements
var temp = ar[j];
ar[j] = ar[j + 1];
ar[j + 1] = temp;
}
}
}
gs.print("Sorted array in ascending order: " + ar);
Reverse Order
To reverse the sorted array, we can simply swap the elements starting from each end of the array.
// Reverse the array
for (var i = 0; i < Math.floor(ar.length / 2); i++) {
var temp = ar[i];
ar[i] = ar[ar.length - 1 - i];
ar[ar.length - 1 - i] = temp;
}
gs.print("Sorted array in descending order: " + ar);
please mark this Helpful and Accepted Solution if this helps you. Your action will help me and the community in understanding same requirements.
Thanks & Regards
Deepak Sharma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2024 09:07 PM
Hi @dvass0107
Please use basic bubble sort method,
Ascending Order
var ar = [5, 3, 7, 1, 10];
for (var i = 0; i < ar.length; i++) {
for (var j = 0; j < ar.length - 1; j++) {
if (ar[j] > ar[j + 1]) {
// Swap elements
var temp = ar[j];
ar[j] = ar[j + 1];
ar[j + 1] = temp;
}
}
}
gs.print("Sorted array in ascending order: " + ar);
var ar = [5, 3, 7, 1, 10];
for (var i = 0; i < ar.length; i++) {
for (var j = 0; j < ar.length - 1; j++) {
if (ar[j] > ar[j + 1]) {
// Swap elements
var temp = ar[j];
ar[j] = ar[j + 1];
ar[j + 1] = temp;
}
}
}
gs.print("Sorted array in ascending order: " + ar);
Reverse Order
To reverse the sorted array, we can simply swap the elements starting from each end of the array.
// Reverse the array
for (var i = 0; i < Math.floor(ar.length / 2); i++) {
var temp = ar[i];
ar[i] = ar[ar.length - 1 - i];
ar[ar.length - 1 - i] = temp;
}
gs.print("Sorted array in descending order: " + ar);
please mark this Helpful and Accepted Solution if this helps you. Your action will help me and the community in understanding same requirements.
Thanks & Regards
Deepak Sharma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2024 10:41 PM
Can you try this?
var ar = [5, 3, 7, 1, 10];
// Bubble Sort implementation
for (var i = 0; i < ar.length; i++) {
for (var j = 0; j < ar.length - 1; j++) {
if (ar[j] > ar[j + 1]) {
var temp = ar[j];
ar[j] = ar[j + 1];
ar[j + 1] = temp;
}
}
}
gs.print("Sorted order: " + ar);