Monday, May 24, 2010

What would this look like in C++ code?

A program that finds the smallest element in the array of integers.

What would this look like in C++ code?
Amanda's answer is good, but it may be slow when your array is very large. Since most data sets are usually larger than N%26gt;=20 the algorithm I am giving is going to be more useful when dealing with an unsorted array. It may seem long but it is a very powerful algorithm. I apologize for the lack of formatting because it won't accept my spaces.





int quicksort(vector %26amp; A, int left, int right)


{


if(left + 10 %26lt;= right){


int pivot = median(A, left, right);


int i = left, j = right-1;


for( , , ){


while(A[++i] %26lt; pivot) {}


while(pivot %26lt; A[--j]) {}


if(i %26lt; j)


swap(A[i], A[j]);


else


break;


}


swap(A[i], A[right - 1]);





quicksort(A, left, i -1); // sort small elements


quicksort(A, i+1, right); // sort large elements


}


else


insertionSort(A, left, right);





/* array is now sorted so you can select any element you want be it the smallest or the largest because the array is sorted */


return A[0];


}





const int median(vector %26amp; A, int left, int right)


{


int center = (left+right)/2;


if(A[center] %26lt; A[left])


swap(A[left], A[center]);


if(A[right] %26lt; A[left])


swap(A[left], A[right]);


if(A[right] %26lt; A[center])


swap(A[center], A[right]);





swap(A[center], A[right-1]);


return A[right-1];


}





void insertionSort(vector %26amp; A)


{


int j;





for(int p=1; p%26lt;A.size(); p++){


int tmp = A[p];


for(j = p; j %26gt; 0 %26amp;%26amp; tmp %26lt; A[j-1]; j--)


A[j] = A[j-1];


A[j] = tmp;


}


}





Now you can easily find minimum numbers in very large arrays with no problem.





I forgot to mention you need to include %26lt;algorithm%26gt; to use the swap function.





PS - All the other solutions posted so far are really stupid because finding the minimum in say 100 million integers is going to take forever. Also one of them is incorrect.
Reply:#include %26lt;algorithm%26gt;





return *std::min_element(array, array + size);





The std library has many useful routines for doing things like searching, sorting and whatnot.





The previous posters gave good information if you need to write it yourself.
Reply:the easiest way is a for loop:





int smallest=array[0];


for(int i=0; i%26lt;array.length; i++){


__if(array[i]%26lt;smallest)


_____smallest=array[i];


}





that will compare every element in the array.





the underscores are cause its easier to read w/ formatting, but yahoo isn't letting me do that right now. :)
Reply:#include %26lt;iostream%26gt;


using namespace std;





int main(){





//This is an int because I like to return 0.





int alphabet[]={12, 2, 7, 6,39}; //hey, I wanted an array.


int SizeOf=5; //It's always better to assign size of array to a variable


// or macro


int Least; // to hold the smallest number.





Least=alphabet[0];





//No braces because I'm only repeating one


//test/action.


for (int i=1;i%26lt;=SizeOf;i++) if (alphabet[i]%26lt;Least) Least=alphabet[i];


// print out the smallest value.


cout %26lt;%26lt; Least %26lt;%26lt; endl;


// tell the OS all is well and bail.


return 0;


}
Reply:int findSmallest(int *arr, int max) {


int smallest = arr[0];


for (int i=1; i%26lt;max; i++)


if (arr[i] %26lt; smallest)


smallest = arr[i];


return smallest;


}
Reply:const int ARRAY_SIZE = 35;


int array[ARRAY_SIZE] = {4,6,2,9,7,0,1,5,7,4,3,7,9,0,1,3,45,6,7,...





int SmallestElementIndex = 0;


int SmallestElement = array[SmallestElementIndex];





for (int i = 0; i %26lt; ARRAY_SIZE; i++)


{


if(array[i] %26lt; SmallestElement)


{


SmallestElement = array[i];


SmallestElementIndex = i;


}


}








This code finds the smallest integer in the array, and also stores the index of that item. If there are multiple elements that have the same value as the smallest integer then the index will be the lowest index (first occurring smallest integer).


No comments:

Post a Comment