Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int main(void)
- {
- int largest;
- int swap = 0;
- int array[ ] = {2, 3, 1, 6, 4, 7};
- int arraySize = 6;
- int i;
- int j;
- for( i = 0; i < arraySize; ++i)
- {
- printf("Array %i is : %i\n", i + 1, array[i]);
- }
- printf("\n\n\n");
- // this outer loop will search through the array by the amount of elements
- for ( i = 0; i < arraySize; ++i)
- {
- largest = array[i];
- // inner loop will check each element against each other
- for ( j = i + 1; j < arraySize; j++)
- {
- if (largest < array[j])
- {
- largest = array[j];
- swap = j;
- }
- }
- // swap the outer element for the largest, and move what was
- // in the outer to the element it was taken from
- array[swap] = array[j];
- array[j] = largest;
- }
- for( i = 0; i < arraySize; ++i)
- {
- printf("Array %i is : %i\n", i + 1, array[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment