Question
C programming Strictly -
Problem 1 (68 points): Write a program to sort an
Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code in C

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <stdio.h>

#include <string.h>

#include <time.h>

void recursiveInsertionSort(int *arr[], int n)

{

int j,key;

if(n<=0)

return;

   else {

recursiveInsertionSort(arr,n-1);

key=*(arr[n]);

int *ar = arr[n];

j=n-1;

   while(j>=0 && *(arr[j])<key)

{

arr[j+1]=arr[j];

j--;

}

arr[j+1]=ar;

}

}

void swap(int *x, int *y) {

   int temp = *x;

   *x = *y;

   *y = temp;

}

int findMax(int *a[],int n) {

   int max;

   if (n == 0)

return 0;

   max = findMax(a,n-1);

   if (*(a[n]) >*(a[max]))

   return n;

   return max;

}

void selectionSort(int *a[],int n) {

   int maxnumber;

if(n<=0)

   return;

else{

maxnumber = findMax(a,n);

swap(a[maxnumber], a[n]);

selectionSort(a,n-1);

   }

}

int main()

{

srand(time(NULL));

int n;

printf("Enter the size of Integer Array\n");

scanf("%d",&n);

int *arr = (int *)malloc(n*sizeof(int));

int *temp = (int *)malloc(n*sizeof(int));

for(int i=0;i<n;++i){

arr[i] = rand()%101;

temp[i] = arr[i];

}

int *ascendingptr[n];

int *descendingptr[n];

for(int i=0;i<n;++i)

ascendingptr[i] =(arr+i);

selectionSort(ascendingptr,n-1);

for(int i=0;i<n;++i)

descendingptr[i] = (arr+i);

recursiveInsertionSort(descendingptr,n-1);

printf("Ascending\tOrignal\tDescending\n");

for(int i=0;i<n;++i)

   printf("\n%d\t \t%d \t \t%d " ,*( ascendingptr[i]),temp[i], *( descendingptr[i]));

}

===================================================================================
Adding Image for more Clarity:

include <stdio.h> #incLude <stdlib.h> #incLude <string.h> #incLude <stdio.h> #incLude <string.h> #incLude «time.h> void recurint main) srand time (NULL)) int n; printf Enter the size of Integer Array\n) scanf (%d,&n) ; int *arr = (int *)malloc(n*Note: I have used rand()%100 --> which will generate random numbers between 0 -100 , Increase this value for larger value of n to get uniform disribution with less duplicates

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <stdio.h>

#include <string.h>

#include <time.h>

void recursiveInsertionSort(int *arr[], int n)

{

int j,key;

if(n<=0)

return;

   else {

recursiveInsertionSort(arr,n-1);

key=*(arr[n]);

int *ar = arr[n];

j=n-1;

   while(j>=0 && *(arr[j])<key)

{

arr[j+1]=arr[j];

j--;

}

arr[j+1]=ar;

}

}

void swap(int *x, int *y) {

   int temp = *x;

   *x = *y;

   *y = temp;

}

int findMax(int *a[],int n) {

   int max;

   if (n == 0)

return 0;

   max = findMax(a,n-1);

   if (*(a[n]) >*(a[max]))

   return n;

   return max;

}

void selectionSort(int *a[],int n) {

   int maxnumber;

if(n<=0)

   return;

else{

maxnumber = findMax(a,n);

swap(a[maxnumber], a[n]);

selectionSort(a,n-1);

   }

}

int main()

{

srand(time(NULL));

int n, i;

printf("Enter the size of Integer Array\n");

scanf("%d",&n);

int *arr = (int *)malloc(n*sizeof(int));

int *temp = (int *)malloc(n*sizeof(int));

for(i=0;i<n;++i){

arr[i] = rand()%101;

temp[i] = arr[i];

}

int *ascendingptr[n];

int *descendingptr[n];

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

ascendingptr[i] =(arr+i);

selectionSort(ascendingptr,n-1);

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

descendingptr[i] = (arr+i);

recursiveInsertionSort(descendingptr,n-1);

printf("Ascending\tOrignal\tDescending\n");

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

   printf("\n%d\t \t%d \t \t%d " ,*( ascendingptr[i]),temp[i], *( descendingptr[i]));

}

Add a comment
Know the answer?
Add Answer to:
C programming Strictly - Write a program to sort an array of integers via arrays of...
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
  • Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and...

    Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and Insertion Sort. The numbers to be sorted will be obtained using a library function which generates pseudo-random numbers. TO Do 1. Fill an array with 140 real numbers between 0.00 and 945.00. Generate the numbers using the subroutine that generates random numbers. Make a spare copy of the array; you will need it later. 2. Call a subroutine to print the contents of the...

  • This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort...

    This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort Heap Sort You have access to the implementation of all of these sorting algorithms, and you may use what is provided in your text directly. Be sure to cite the source of these implementations in the header of your program. Please maintain the property that these sorting algorithms sort arrays in ascending order. For this homework, you will write a...

  • create a file homework_part_1.c a) Implement the function initialize_array that receives two parameters: an array of...

    create a file homework_part_1.c a) Implement the function initialize_array that receives two parameters: an array of integers and the array size. Use a for loop and an if statement to put 0s in the odd positions of the array and 5s in the even positions. You must use pointers to work with the array. Hint: review pointers as parameters. b) Implement the function print_array that receives as parameters an array of integers and the array size. Use a for statements...

  • C programing Write a program to sort numbers in either descending or ascending order. The program...

    C programing Write a program to sort numbers in either descending or ascending order. The program should ask the user to enter positive integer numbers one at a time(hiting the enter key after each one) The last number entered by the user should be -1, to indicate no further numbers will be entered. Store the numbers in an array, and then ask the user how to sort the numbers (either descending or ascending). Call a function void sortnumbers ( int...

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays....

    Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...

  • use c program Normal No Spac. Heading 1 Heading 2 se Edit Paragraph Styles Lab 7...

    use c program Normal No Spac. Heading 1 Heading 2 se Edit Paragraph Styles Lab 7 Ascending and/or descending array using bubble sort and pointer Sort a set of integer numbers in ascending and/or descending orders using the bubble sort and pointers. You must use bubble sort and pointers to sort an array. You can use static or dynamic array

  • C++ Write a program that can be used to compare Insertion Sort, Merge Sort and Quick...

    C++ Write a program that can be used to compare Insertion Sort, Merge Sort and Quick Sort. Program must: Read an array size from the user, dynamically an array of that size, and fill the array with random numbers Sort the array with the Insertion Sort, MergeSort and QuickSort algorithms studied in class, doing a time-stamp on each sort. Use your program to measure and record the time needed to sort random arrays of size 5000, 50000, and 500000. For...

  • Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers

    Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

  • Write a program to merge two sorted arrays into a third array. Ask the user to...

    Write a program to merge two sorted arrays into a third array. Ask the user to enter the size of the two arrays. The length of array1 could be greater than, equal to or less than the length of array2. Fill both with random numbers 0-99. Sort both arrays as explained below. Write a method merge that takes the two arrays as arguments. The method returns a merged array with size equal to the size of both arrays combined. Note:...

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