“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
Read more comments...
Implement an algorithm to sort an array.
2,529 Views | (3 votes, avg: 4.33)
use a stack
An array can be sorted using any of the classic algorithms like quicksort , heapsort and the inefficient bubblesort.
For use with strings and int/floats/doubles
while (!sorted){ sorted=true; for(int i=1; i<size; i++) { if(arr[i]<arr[i-1]) { sorted=false; tmp=arr[i]; arr[i]=arr[i-1]; arr[i-1]=tmp; } }}
int[] arraySort(int[] arr, int len){ for (int i=0;i<len;i++) { if(arr[i]>arr[j]) { int buf=arr[i]; arr[i]=arr[j]; arr[j]=buf; } } return arr;}
Simple Insertion Sort.
Sorry in last solution forgot to add a loop. Here is the correct solution.
int[] arraySort(int[] arr, int len){for (int i=0;i<len;i++){for (int j=i+1; j<len; j++){if(arr[i]>arr[j]){int buf=arr[i];arr[i]=arr[j];arr[j]=buf;}}return arr;}
Use a quick sort if it is a fairly large array , other wise a merge sort should solve the purpose.
Try this,
void sortarray(int *ar, int len){
for (int i=0 ;i < len; i++){ for (int j =i+1 ;j < len ;j++) if(ar[j]< ar[i]) { int tmp = ar[i]; ar[i] = ar[j]; ar[j] =tmp; }}
//print to test (after sorting)for(int i =0 ;i < len ;i++) cout<< ar[i] << “n”;
}
i remmeber reading somewhere that heapsort is the best for arrays, or is it that heapsort can only be used for arrays?
What is the matter with you people? That’s not insertion sort.. it’s plain old stupid bubble sort.
Name (required)
Mail (will not be published) (required)
Your Answer
To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
use a stack
An array can be sorted using any of the classic algorithms like quicksort , heapsort and the inefficient bubblesort.
For use with strings and int/floats/doubles
while (!sorted)
{
sorted=true;
for(int i=1; i<size; i++)
{
if(arr[i]<arr[i-1])
{
sorted=false;
tmp=arr[i];
arr[i]=arr[i-1];
arr[i-1]=tmp;
}
}
}
int[] arraySort(int[] arr, int len)
{
for (int i=0;i<len;i++)
{
if(arr[i]>arr[j])
{
int buf=arr[i];
arr[i]=arr[j];
arr[j]=buf;
}
}
return arr;
}
Simple Insertion Sort.
Sorry in last solution forgot to add a loop. Here is the correct solution.
int[] arraySort(int[] arr, int len)
{
for (int i=0;i<len;i++)
{
for (int j=i+1; j<len; j++)
{
if(arr[i]>arr[j])
{
int buf=arr[i];
arr[i]=arr[j];
arr[j]=buf;
}
}
return arr;
}
Simple Insertion Sort.
Use a quick sort if it is a fairly large array , other wise a merge sort should solve the purpose.
Try this,
void sortarray(int *ar, int len)
{
for (int i=0 ;i < len; i++)
{
for (int j =i+1 ;j < len ;j++)
if(ar[j]< ar[i])
{
int tmp = ar[i];
ar[i] = ar[j];
ar[j] =tmp;
}
}
//print to test (after sorting)
for(int i =0 ;i < len ;i++)
cout<< ar[i] << “n”;
}
i remmeber reading somewhere that heapsort is the best for arrays, or is it that heapsort can only be used for arrays?
What is the matter with you people? That’s not insertion sort.. it’s plain old stupid bubble sort.
Leave an Answer/Comment