i have a array
{10,20,30,40,50 and so on}
and i want that this array be sorted in the form {20,10,40,30,60,50......}
how can i do this???
plz write the code i am unable to solve this.....
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jignesh TrivediPosted Feb 27, 2014, 4:07 AM
try....
int[] a = new int[10] { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
int count = (a.Length / 2) - 1;
int swapDataIndex = 0;
for (var arrayData = 0; arrayData <= count; arrayData++)
{
int dummyNo = a[swapDataIndex + 1];
a[swapDataIndex + 1] = a[swapDataIndex];
a[swapDataIndex] = dummyNo;
swapDataIndex += 2;
}
foreach (var datainArray in a)
{
Console.WriteLine(datainArray);
}
hope this will help you.
Vipin TyagiPosted Feb 27, 2014, 6:59 AM
VulpesPosted Feb 27, 2014, 6:08 AM
The output is:
20 10 40 30 60 50 80 70 100 90
Vipin TyagiPosted Feb 27, 2014, 2:51 AM
and we want to swap 1st element to 2nd ,3rd element with 4th 5th with 6 and so on.......
means if array is
1[0] index number
2[1]
3
4
5
6
7
8
9
10[9]
after swapping the element will be
2[0]index
1
4
3
6
5
8
7
10
9[9]index
Lakshmanan Sethu SankaranarayanPosted Feb 27, 2014, 12:21 AM