Question

The following code snippet is for C++ int selection_Sort(int A[ ], int n) { int I,...

The following code snippet is for C++

int selection_Sort(int A[ ], int n)

{

int I, j, small, temp;

for( i = 0; i < n-1; i++)

{

small = i;

for(j = i + 1; j < n; j++)

{

if ( A[ j ] < A[ small ]

small = j;

}

temp = A [ i ];

A[ i ] = A[small];

A[small] = temp;

}

}

Please explain in rich detail the logic behind every execution and why it does what it does according to the format that the code has! I am really trying to understand how this all works and runs! Thank you in advance!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please upvote if you like this answer.

CODE:

#include <iostream>
using namespace std;
int selection_Sort(int A[], int n)
{
   int i, j, small, temp;
   for( i = 0; i < n-1; i++)
   {
     small = i;
     for(j = i+1; j < n; j++)
     {
       if ( A[j] < A[small])
       {
          small = j;
       }
    }
      temp = A [i];
      A[i] = A[small];
      A[small] = temp;
   }   
}
int main()
{
    int arr[] = {5,6,2,3,1};
   cout << "given array is : 5,6,2,3,1 \n" ;
    int n = sizeof(arr)/sizeof(arr[0]);
   int i;
    selection_Sort(arr, n);
    cout << "Sorted array is: \n";
   for (i=0; i < n; i++)
        cout << arr[i] << " ";
    
    return 0;
}
OUTPUT:

EXPLANATION:

Add a comment
Know the answer?
Add Answer to:
The following code snippet is for C++ int selection_Sort(int A[ ], int n) { int I,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What's the conversion from C++ to assembly code x86 int findMinIndex (int integer_array[], int i, int...

    What's the conversion from C++ to assembly code x86 int findMinIndex (int integer_array[], int i, int j) { // C code to be converted to x86 assembly /* int iMin = i; // test against elements after i and before j to find the smallest for ( i ; i < j; i++) { // if this element is less, then it is the new minimum    if (integer_array[i] < integer_array[iMin]) { // found new minimum; remember its index iMin...

  • 1). What is the complexity of the following code snippet? { for (int count2 = 0;...

    1). What is the complexity of the following code snippet? { for (int count2 = 0; count2<n; count2++) { /*some sequence of O(1) step*/ } } select one: a. O(N^2) b. O(Log N) c. O(1) d. O(N!) 2). What is the complexity of the following code snippet? for (int count = 0; count<n; count++) { printsum(count) } select one: a. We need to know the complexity of the printsum() function. b. O(Log N) c. O(1) d. O(N) e. O(N^2) 3)....

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • Consider the following C++ code segment: for (int i = 0; i <n; ++i) { for...

    Consider the following C++ code segment: for (int i = 0; i <n; ++i) { for (int j = 0; j <m; ++j) if (i != j) cout << "0"; else cout << "1"; } } Which of the options below gives the correct output if the value of nis 2and the value of mis 3? 1. 100010 2. 011101 3. 100100 4. 010001

  • I am trying to write the following code using pointers and traversal by pointers instead of...

    I am trying to write the following code using pointers and traversal by pointers instead of indexing (in C++). The following code is correct: void shift(int arr[], int n) { int temp = arr[n - 1]; int temp1; for (int i = 0; i < n; i++) { temp1 = arr[i]; arr[i] = temp; temp = temp1; } } This is my ATTEMPT at writing it with pointers (and traversal by pointers!) but I know it is wrong. I believe...

  • Consider the following code C++ like program: int i, j, arr[5]; //arr is an array starting...

    Consider the following code C++ like program: int i, j, arr[5]; //arr is an array starting at index 0 void exchange(int x, int y) { int temp:= x; x:= y; y:= temp; } main(){ for (j = 0; j < 5; j++) arr[j]:= j; i:= 1; exchange(i, arr[i+1]); output(i, arr[2]); //print i and arr[2] } What is the output of the code if both parameters in function swapping are passed by: a- value? b- reference? c- value-result?

  • (a)How many times does the code snippet given below display "Hello"? int x = 1; while...

    (a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) {    System.out.println ("Hello");    x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) {    sum = sum + i;    i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...

  • c++ help Write a program that obtains the execution times for the following three code segments...

    c++ help Write a program that obtains the execution times for the following three code segments with the different n. code 1: for(int i = 0; i <= n; i++)   { x = i; } code 2: for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { x = i + j; } } code 3: for (int i = 0; i <= n; i++) { for (int j =...

  • Consider the following code: Void F1 (int n) { int a; for(int i = 0; i...

    Consider the following code: Void F1 (int n) { int a; for(int i = 0; i < n; i += 2) a = i; } Which of the following characterization, in terms of n, of the running time of the above code (F1) is correct? Θ(n3/2)                   · O(1/n)                 · O(n)                     · Ω(n2) Consider the following code: Void F1 (int n) { int a; for(int i = 0; i < n; i += 2) a = i; }...

  • Consider the following algorithms int add.them (int n, int AI) index iふk; j=0; for (i =...

    Consider the following algorithms int add.them (int n, int AI) index iふk; j=0; for (i = 1; n; i++) jsjtA[i]; for (i = 1; n; i++) return j+ int anysqual (int n, int A) index i,j,k.m; for ( iSn i++) for G-1:jSnj++) for (k = 1; k n: k++) for (m t= 1; m n: m++) return 1 return 0 Note: The array parameter A[ I[1 in any equal is an n x n two-dimensional array. For example when n...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT