Hi,

All of you must have come across statements and questions online or technical exams or placement exams asking for the

The Code is easy to understand and implement.

Do Leave Feed backs.

Thanks

Piyush

All of you must have come across statements and questions online or technical exams or placement exams asking for the

__. Below I have shared a code to do that.__**MOST OPTIMIZED Code**for sortingThe Code is easy to understand and implement.

*#include <stdio.h>*

#include <math.h>

void createArray(int *array_players, int nlenght);

void createTournament(int *arry_players, int nlenght);

int findNextMax(int arry_players[], int nlenght);

int main()

{

int i, arry_players[100], nlenght, max_value, nextMax_value, sorted_array[100];

printf("Enter Number of Elements in Array ");

scanf("%d",&nlenght);

createArray(arry_players, nlenght);#include <math.h>

void createArray(int *array_players, int nlenght);

void createTournament(int *arry_players, int nlenght);

int findNextMax(int arry_players[], int nlenght);

int main()

{

int i, arry_players[100], nlenght, max_value, nextMax_value, sorted_array[100];

printf("Enter Number of Elements in Array ");

scanf("%d",&nlenght);

createArray(arry_players, nlenght);

for(i = 1; i <= nlenght - 1; i++)

{

createTournament(arry_players, nlenght);

max_value = arry_players[1];

nextMax_value = findNextMax(arry_players, nlenght);

sorted_array[i-1] = max_value;

sorted_array[i] = nextMax_value;

}

for(i = 0; i < nlenght ; i++)

printf(" %d ",sorted_array[i]);

}for(i = 1; i <= nlenght - 1; i++)

{

createTournament(arry_players, nlenght);

max_value = arry_players[1];

nextMax_value = findNextMax(arry_players, nlenght);

sorted_array[i-1] = max_value;

sorted_array[i] = nextMax_value;

}

for(i = 0; i < nlenght ; i++)

printf(" %d ",sorted_array[i]);

}

**/********************************************//****

* \ This method is used to construct an array in which we are passing the address of source array

and using the address we are changing the value of array at that address

*

* \ *array_players = pointer to array arry_players

* \ nlenght = lenght of Array entered

* \ return null/void

*

***********************************************/* \ This method is used to construct an array in which we are passing the address of source array

and using the address we are changing the value of array at that address

*

* \ *array_players = pointer to array arry_players

* \ nlenght = lenght of Array entered

* \ return null/void

*

***********************************************/

void createArray(int *arry_players, int nlenght)

{

int i, temp;

for(i = 1; i<= nlenght; i++)

{

scanf("%d", &temp);

arry_players[2*nlenght - i] = temp;

}

}void createArray(int *arry_players, int nlenght)

{

int i, temp;

for(i = 1; i<= nlenght; i++)

{

scanf("%d", &temp);

arry_players[2*nlenght - i] = temp;

}

}

**/********************************************//****

* \ This method is used to construct a tournament in which we are passing the address of source array

and using the array we are creating a competition and comparing every two consequitive players

and finding the winner or the maximum value

*

* \ *array_players = pointer to array arry_players

* \ nlenght = lenght of Array entered

* \ return null/void

*

***********************************************/* \ This method is used to construct a tournament in which we are passing the address of source array

and using the array we are creating a competition and comparing every two consequitive players

and finding the winner or the maximum value

*

* \ *array_players = pointer to array arry_players

* \ nlenght = lenght of Array entered

* \ return null/void

*

***********************************************/

void createTournament(int *arry_players, int nlenght)

{

int i;

for(i = 2*nlenght - 1; i >= 3; i = i - 2)

{

if(arry_players[i] > arry_players[i-1]) arry_players[i/2] = arry_players[i];

else arry_players[i/2] = arry_players[i - 1];

}

//for(i = 1; i <= 2* nlenght - 1; i++)

//printf("%d ",arry_players[i]);

printf("\n\n");

}void createTournament(int *arry_players, int nlenght)

{

int i;

for(i = 2*nlenght - 1; i >= 3; i = i - 2)

{

if(arry_players[i] > arry_players[i-1]) arry_players[i/2] = arry_players[i];

else arry_players[i/2] = arry_players[i - 1];

}

//for(i = 1; i <= 2* nlenght - 1; i++)

//printf("%d ",arry_players[i]);

printf("\n\n");

}

**/********************************************//****

* \ This method is used to traverse backwards in the tournament along the path of the max value and convert max value into 0

and find the next max value

*

* \ *array_players = pointer to array arry_players

* \ nlenght = lenght of Array entered

* \ return null/void

*

***********************************************/* \ This method is used to traverse backwards in the tournament along the path of the max value and convert max value into 0

and find the next max value

*

* \ *array_players = pointer to array arry_players

* \ nlenght = lenght of Array entered

* \ return null/void

*

***********************************************/

int findNextMax(int *arry_players, int nlenght)

{

int i = 2, nextmax = 0, max_value;

max_value = arry_players[1];

arry_players[1] = 0;

while(i < (2*nlenght - 1))

{

if(arry_players[i] == max_value )

{

arry_players[i] = 0;

if(nextmax < arry_players[i + 1])

nextmax = arry_players[i + 1];

i = i;

}

else if(arry_players[i + 1] == max_value)

{

arry_players[i + 1] = 0;

if(nextmax < arry_players[i])

nextmax = arry_players[i];

i = i + 1;

}

i = i * 2;

}

return nextmax;

}int findNextMax(int *arry_players, int nlenght)

{

int i = 2, nextmax = 0, max_value;

max_value = arry_players[1];

arry_players[1] = 0;

while(i < (2*nlenght - 1))

{

if(arry_players[i] == max_value )

{

arry_players[i] = 0;

if(nextmax < arry_players[i + 1])

nextmax = arry_players[i + 1];

i = i;

}

else if(arry_players[i + 1] == max_value)

{

arry_players[i + 1] = 0;

if(nextmax < arry_players[i])

nextmax = arry_players[i];

i = i + 1;

}

i = i * 2;

}

return nextmax;

}

Do Leave Feed backs.

Thanks

Piyush

## No comments:

## Post a Comment