- 1
How can i write a function to arrange an array !
5 Answers
+ 1
depends how you want it arranged
+ 1
hello Hiba...
well, there are lot of ways you can arrange your array.
but methods depends upon what kind of arrangement you require.
for sorting (asc. or decs.)
there are number of algo available...
you may refer google, books. whatever you like.
and if you need a specific method of a problem.
write to us the problem question.
+ 1
array=[4,1,6,3,8,5]
array=sorted(array)
print(array)
+ 1
here i am providing the technique called SELECTION SORT.
in this sort we select the smallest value of the unsorted part of array and replace it with the sorted part at suitable index here is the code...
#include <iostream>
using namespace std;
int main() {
int arr[]= {2,5,6,7,3,1,9,8};
int i,j,size=8,small, pos=0, temp;
/* you can also take the input at run time using loops */
for(i=0;i<size;++i) //run size times
{small = arr[i]; //give a new value to temp
pos=i;
for(j=i;j<size;++j)//finds the smallest value
{
if(small>arr[j])
{small=arr[j];
pos=j;}
}
temp = arr[i]; //swap with smallest value
arr[i] = arr[pos];
arr[pos] = temp;
}
//now print array and see your sorted array.
for(i=0;i<size;++i)
{
cout<<arr[i]<<" ";
}
return 0;
}
you may check its working by going in the code posts area and searching for most recent in c++ "selection sort array by CodeRunner".
0
char arr[5] ;
i want to arrange it's elements from the smaller to bigger