Question

..ooo Simple LTE 10:29 PM 40% E idterm 1 sample question.pdf N Open with Print 5. (20 points) Sort the list of nut through each step of the algorithm. The algorithm for selection sort is as follows: i. Find the smallest em in a list. ii. Swap this value with the value currently at the front of the list. iii. Repeat Steps 1 and 2 with the current size of the list minus one (list size Iist size 1)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

void swap(int *first, int *second)
{
int temp = *first;
*first = *second;
*second = temp;
}

//print an array
void print(int array[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", array[i]);
printf("\n");
}

void selectionSort(int array[], int size)
{
int i, j, min;

// moving boundary of unsorted array one by one
for (i = 0; i < size-1; i++)
{
//finding minimum number in array
min = i;
for (j = i+1; j < size; j++)
if (array[j] < array[min])
min = j;
print(array, size);
// swapping minimum element with the first element
swap(&array[min], &array[i]);
}
}

int main()
{
int array[] = {35, 12, 6, 23, 18};
int size = sizeof(array)/sizeof(array[0]);
printf("Array: \n");
selectionSort(array, size);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Sort the list of numbers: 35, 12, 6, 23, 18 using Selection sort. Work through each...
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
  • PYTHON Fill in 3 numbers to complete the following 'list', as shown: 35 62 ___ 40...

    PYTHON Fill in 3 numbers to complete the following 'list', as shown: 35 62 ___ 40 ___ 18 ___ 1 55 28 add the month, day, and year you were born in the 3 spaces shown (in that order). (Use a 4-digit year.) Using the list of numbers above, write 4 different sort trace tables like those produced by the practice program linked above: Sort using the BubbleSort algorithm in ascending order. Sort using the BubbleSort algorithm in descending order....

  • [5 marks] Using selection sort algorithm to sort the array int array[7]-5, 6, 2, 7, 9,...

    [5 marks] Using selection sort algorithm to sort the array int array[7]-5, 6, 2, 7, 9, 4, 3). Assuming we call the following function using statement selection sort (int array, 7); Please fill the table to show the changes of the array int_array after each iteration. The first column is filled. void selection_sort (int list[], int list_size) for (int i = 0; i < list size - 1; 1++) int current min = list[1]; int current_min_index-i for (int j -...

  • 2. Show the steps when sorting (smallest to largest) the following arrays of numbers using selection...

    2. Show the steps when sorting (smallest to largest) the following arrays of numbers using selection sort i.e. every time a swap occurs, show what the current state of the array looks like, including the final sorted array. -12 points a. [10,2,5,8,9,1,4,7) b. 17,1,3,2,5, 4, 8, 12, 9) c. 18,7,6,5, 4, 3, 2, 1) d. 15,3, 8, 1, 9, 4, 2, 6) I

  • JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers:...

    JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers: 47 71 15 35 66 61 44 26 68 56 18 19 36 84 69 55 1. Find the value of pivot 2. Show the result after partitionIt() is called first time 3. Show the value of partition 4. Show the content of the array ///////////////////////////// Lab6.java class ArrayIns { private long[] theArray; // ref to array theArray private int nElems; // number of...

  • Directions: Problem 1: Write (using pen-and-paper rather than code) the list after each pass of quick...

    Directions: Problem 1: Write (using pen-and-paper rather than code) the list after each pass of quick and merge sort for the following list of numbers. Assume that you are sorting the numbers into ascending order. For quick sort, assume that the first number from the sublist is chosen as the pivot. 54 17 21 18 4 7 19 41 Problem 2: Write the list after each pass of the quick sort algorithm for the following list of numbers, using the...

  • Write a Java program with a single-dimension array that holds 11 integer numbers and sort the...

    Write a Java program with a single-dimension array that holds 11 integer numbers and sort the array using a bubble sort. Next, identify the median value of the 11 integers. Here are the steps your program must accomplish. algorithm (either flowchart or pseudocode) that you will use to write the program Step 1. Create an Place the algorithm in a Word document. 6. Ste the Step 2. Code the program in Eclipse and ensure the following steps are accomplished. 1....

  • 1.             (5 marks) Perform a selection sort on the list 23, 59, 34, 13, 31, 10. Show...

    1.             (5 marks) Perform a selection sort on the list 23, 59, 34, 13, 31, 10. Show the list after each exchange that has an effect on the list ordering. 2.             (5 marks) Explain why the bubble sort algorithm does Ө(n2) comparisons on an n-element list. The bubble-sort algorithm is shown just after question 10 on p. 141. 3.              (5 marks) Write the resulting data list, give the ending value of legit, and find the exact number of copies done by the converging...

  • Programming language: Java Home Work No.2 due 09.11.2019 either ascending or descending SORTING: An array is...

    Programming language: Java Home Work No.2 due 09.11.2019 either ascending or descending SORTING: An array is said to be ordered if its values order. In an ascending ordered array, the value of each element is less than or equal to the value of the next element. That is, [each element] <= [next element]. A sort is an algorithm for ordering an array. Of the many different techniques for sorting an array we discuss the bubble sort It requires the swapping...

  • 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...

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