0
how to rearrange array in order smallest, largest, 2nd smalles, 2nd largest.
i would like to know how to rearrange array in order smallest, largest, 2nd smalles, 2nd largest. without using functions if possible
25 Réponses
+ 3
Do you mean the syntax? or understanding it?
this is an rewrite of the above code in a simpler way:
var firstList=[7,5,3,8,3,0,1,7];//the list of the numbers.
var newList=[]; //the list that we will put the numbers in it in our order.
for( ;firstList.length>0; ){ // here I make a for loop that will still working if there are any elements in the first array because we will remove them two by two.
var maxNumber=Math.max(...firstList);//Math.max method returns us the largest value of the givine values and the "..." before the list name removes the "[" and "]" .
newList.push(maxNumber);//then we add the max value to the new list.
var indexOfMax= firstList.indexOf(maxNumber); //we get the index of the max number using the indexOf() to know where it is for removing it.
firstList.splice(indexOfMax,1); // here we use splice() to cut from the index of the max number to the one index after it (results in removing only one element)
//here we repeat the same proccess with the smallest number
var minNumber=Math.
+ 1
If you not understand the logic by @Mohamed which is in javascript, you can do this way in c/c++/java
First take values into array, then sort array. Now create a new array of same length (or if you need not store then just print) and let I=0,j = array.lengyh-1 then pick items from sorted array at index I and j which are min, max values from array..
Increament I, decrement j
Repeat the loop until i<=j
Hope it helps..
+ 1
copy paste it in an colored editor to make it easy to read
+ 1
Fatima Omar
int arr[] = { 1,2,3,4,5 }; //array is in sorted order already. If not then first sort the array... Then apply next,..
int narr[] = new int[5];
for(int i=0, j=arr.length-1,k=0; i<=j ; i++, j--, k+=2)
if(i != j) {
narr[k]=arr[i];
narr[k+1]=arr[j];
}
else
narr[k]=arr[i];
Now you narr[] array has elements as you want.
If you don't understand, just try your own approach and try to code as much as possible.. then ask here for the issues with details, along with your try...
Hope it helps..
+ 1
There are several sorting algorithms. The most known are Bubble, Merge, Tim, Quick, Insertion, Bucket, Heap and Selection Sort.
They all have advantages and disadvantages and it depends how your list looks which one to use.
+ 1
Currently it's infinite while loop because You missed to add i++.
But code looks fine if you enter sorted input
Test this input:
4 1 2 3 4
Output I got: 2 1 4 3 which is correct one only..
What is your input?
Ohh. start swapping from index one instead of 0.
edit:
Fatima Omar
why you need to check even odd indexes..?
just start swapping from index 1
swap values at 1,2 then 3,4 , then...
increment each time I by 2 i+=2;
repeat this until i<n;
before all these, you need to sort values in according order.. like 2,4,3,5 to 2,3,4,5 then only solution works...otherwise you need to find other solution..
look at bubble sort algorithm which is easy to understand... .
you can simply do like this in current code :
i=1
while( i<n) {
int t = a[i];
a[i] = a[ i+1];
a[i+1] = t;
i = i+2;
}
0
make a new array and push into it the smallest and largest numbers of the first array amd remove the from the first one
then repeat it by a for loop
0
but what if i do not know the number like i would ask the user to input them
0
numbers*
0
you do not need to know
for loop will deal with that:
var l=[7,5,3,8,3,0,1,7]; //example array
var n=[] //the new list
for( ;l.length>0; ){
n.push(Math.max(...l)); l.splice(l.indexOf(Math.max(...l)),1);
n.push(Math.min(...l)); l.splice(l.indexOf(Math.min(...l)),1);
}
console.log(n);
Note: if the length of the array is even number then there will be a null value in the end of the list, you can add an if statment to check and handling that
0
can someone help with the typing the code itself because my level is still not that good so i dont understand well except by code
0
OH!!!
I have relized just now that ypu are not talking about javascript, Sorry
0
idk guys maybe if i gave you the main question there will be a link between us to understand cause i still do not understand lol.
the question says:
You have an array of distinct integer numbers; rearrange these numbers in ascending
order such that x2- x1>0, X3 – X2 <0, x4-x3 >0 , X5– X4 <0 and so on.
Input: the first line of the input will contain integer n (3<=n<=10^3) the total of
numbers in the group. Then you will get n numbers (x1, X2, X3, X4, ...) where (1 <=x<=
10^3).
Output: print the array sorted in any order that satisfy the problem.
Example #1
Input
5
12345
Output
1 3254
Example #2
Input
6
175 99 145 1 666
Output
I 17 5 145 99 666.
i want it to be in c/c++ so i could function it well.
and thanks i really apprecitae what you guys did so far, it is a great work, but it is me that still doesnt understand lol.
0
Input
5
12345 (already in sorted order)
Output: 13254 it just swapping each pairs from index 1
1 23 45 => 1 32 54
And also your approach also works...
12345 => pick x1=1 , x2=5, x3=2, x4=4 x5 =3
(Pick first1, last1, first2, last2,...)
x2- x1>0, 5-1>0
X3 – X2 <0, 2-5<0
x4-x3 >0 , 4-2>0
X5– X4 <0 3-4>0
all satisfied here
So it is helpful, instead of telling not understanding, mention detailly where you not understand, and what upto you understood...
The above code works almost same, just array initialization and length finding is different in c/c++..
edit:
Fatima Omar second example
17 , 5 , 99 , 145 , 1 , 666
Sort it ,then array will be
1 , 5 , 17, 99, 145, 666
Swap 5,17 next 99,145
then array will become
1, 17, 5 , 145 , 99 ,666
with the logic, I think you can implement code now..
Hope it helps...
0
Jayakrishna🇮🇳 thank you so much. should i create a function to compare the first elemnt with the next and if jt is greater to swap them ?
0
Abdul Wasay
sorry but what is bubble sorting and how it should br done?
0
Fatima Omar You can do any type as you need or your comfort..
But here in simple way, after sorting array, use a loop and from index 1 to last, swap pairs in loop itself..
0
Jayakrishna🇮🇳
#include <stdio.h>
int main()
{
int x, y, n, i=0;
scanf("%d", &n);
int a[1000];
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
while (i < n){
if ((i+1)%2 == 0)
{
x= a[i] ;
y= a[i + 1] ;
if (a[i] > a[i + 1])
{
a[i] = y;
a[i + 1] = x;
}
}
else
{
x= a[i] ;
y= a[i + 1] ;
if (a[i] < a[i + 1])
{
a[i] = y;
a[i + 1] = x;
}
}
}
for (int i = 0; i < n; i++)
printf("%d\t", a[i]);
}
is it right this way?
0
Jayakrishna🇮🇳
so i pressed run and it printed the same numbers of the array that user entered, could you find out the problem?
0
really thank you i did this and it worked!!!.
but i have another problem which is if i have an array and i want to find the longest consecutive sequence how to do so?
for example i have
11,1,9,25,1,66
the longest duration is 3 because we have from 1,9,25 1,66 is smaller so we take the longest one, how to do this with code if the user will input the array size and elemnts. i thought of picking a number from the middle of the array and to then compare from the middle, what do you think?