Question

Selection sort is often the first sorting algorithm covered in introductory computer science courses. Java code...

Selection sort is often the first sorting algorithm covered in introductory computer science courses. Java code that uses selection sort to place the elements of an integer array into non-decreasing order is shown here:
public void swapNumbers(int i, int j)
{
​int temp = numbers[i];​ /* put numbers[i] somewhere to
keep it safe */
​numbers[i] = numbers[j]; /* put numbers[j] at index i */
​numbers[j] = temp;​ /* put numbers[i] at index j */
}
public void selectionSort(int[] numbers)
{
/* For every index in the array,
with the exception of the last one, */
for (int searcherIndex = 0; searcherIndex < numbers.length-1;
searcherIndex++)
{
​/* Assume that the number is where it's supposed to be */
​int correctIndex = searcherIndex;
​/*Try out other candidate indexes*/
​for (int candidateIndex = searcherIndex+1;
candidateIndex < numbers.length; candidateIndex++)
​{
​​/* If you find a smaller number,
make it's index the correct index */
​​if(numbers[candidateIndex] < numbers[correctIndex])
​​correctIndex = candidateIndex;
​}
​/* At this point correctIndex really will be
the correct index */
​swapNumbers (searcherIndex, correctIndex);
}
}
A similar C++ version (from Dr. Mock’s notes) and an example driver program is shown here:
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
void selectionSort(int a[], int n)
{
for (int i = 0; i < n-1; i++)
{
int indexOfMin = i;
for (int j = i+1; j < n; j++)
{
if (a[j] < a[indexOfMin])
indexOfMin = j;
}
swap(a[i], a[indexOfMin]);
}
}
int main()
{
int a[] = {4, 5, 1, 354, 21, 45, 3};
for (int i = 0; i < 7; i++)
cout << a[i] << endl;
selectionSort(a, 7);
for (int i = 0; i < 7; i++)
cout << a[i] << endl;
}
For this assignment, you are required to translate one of these two high-level-language algorithms into a MIPS assembly language program that uses subroutines selectionSort and swap to sort an array of integers. Use the selected algorithm as in-line documentation for your MIPS source code. Be sure that both subroutines follow standard register use conventions, and utilize the stack frame technique shown in class for all subroutine calls. Your main method should use system calls to interactively display the contents of the array both before and after the call to selectionSort.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
.file    1 ""
    .section .mdebug.abi32
    .previous
    .nan    legacy
    .module    fp=32
    .module    nooddspreg
    .abicalls
    .text
    .align    2
    .globl    swap
    .set    nomips16
    .set    nomicromips
    .ent    swap
    .type    swap, @function
swap:
    .frame    $sp,0,$31        # vars= 0, regs= 0/0, args= 0, gp= 0
    .mask    0x00000000,0
    .fmask    0x00000000,0
    .set    noreorder
    .set    nomacro
    j    $31
    nop

    .set    macro
    .set    reorder
    .end    swap
    .size    swap, .-swap
    .align    2
    .globl    selectionSort
    .set    nomips16
    .set    nomicromips
    .ent    selectionSort
    .type    selectionSort, @function
selectionSort:
    .frame    $sp,0,$31        # vars= 0, regs= 0/0, args= 0, gp= 0
    .mask    0x00000000,0
    .fmask    0x00000000,0
    .set    noreorder
    .set    nomacro
    j    $31
    nop

    .set    macro
    .set    reorder
    .end    selectionSort
    .size    selectionSort, .-selectionSort
    .section    .rodata.str1.4,"aMS",@progbits,1
    .align    2
$LC1:
    .ascii    "%d\000"
    .rdata
    .align    2
$LC0:
    .word    4
    .word    5
    .word    1
    .word    354
    .word    21
    .word    45
    .word    3
    .section    .text.startup,"ax",@progbits
    .align    2
    .globl    main
    .set    nomips16
    .set    nomicromips
    .ent    main
    .type    main, @function
main:
    .frame    $sp,80,$31        # vars= 32, regs= 5/0, args= 16, gp= 8
    .mask    0x800f0000,-4
    .fmask    0x00000000,0
    .set    noreorder
    .cpload    $25
    .set    nomacro
    addiu    $sp,$sp,-80
    lw    $5,%got($LC0)($28)
    lw    $25,%call16(memcpy)($28)
    sw    $18,68($sp)
    addiu    $18,$sp,24
    movz    $31,$31,$0
    .cprestore    16
    sw    $19,72($sp)
    sw    $17,64($sp)
    sw    $16,60($sp)
    sw    $31,76($sp)
    li    $6,28            # 0x1c
    addiu    $5,$5,%lo($LC0)
    .reloc    1f,R_MIPS_JALR,memcpy
1:    jalr    $25
    move    $4,$18

    lw    $28,16($sp)
    move    $17,$0
    lw    $16,%got($LC1)($28)
    li    $19,28            # 0x1c
    addiu    $16,$16,%lo($LC1)
    addu    $2,$18,$17
$L10:
    lw    $25,%call16(__printf_chk)($28)
    lw    $6,0($2)
    move    $5,$16
    li    $4,1            # 0x1
    .reloc    1f,R_MIPS_JALR,__printf_chk
1:    jalr    $25
    addiu    $17,$17,4

    lw    $28,16($sp)
    bne    $17,$19,$L10
    addu    $2,$18,$17

    move    $17,$0
    li    $19,28            # 0x1c
    addu    $2,$18,$17
$L11:
    lw    $25,%call16(__printf_chk)($28)
    lw    $6,0($2)
    move    $5,$16
    li    $4,1            # 0x1
    .reloc    1f,R_MIPS_JALR,__printf_chk
1:    jalr    $25
    addiu    $17,$17,4

    lw    $28,16($sp)
    bne    $17,$19,$L11
    addu    $2,$18,$17

    lw    $31,76($sp)
    lw    $19,72($sp)
    lw    $18,68($sp)
    lw    $17,64($sp)
    lw    $16,60($sp)
    move    $2,$0
    j    $31
    addiu    $sp,$sp,80

Add a comment
Know the answer?
Add Answer to:
Selection sort is often the first sorting algorithm covered in introductory computer science courses. Java code...
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
  • I need help of how to include the merge sort code into this counter code found...

    I need help of how to include the merge sort code into this counter code found below: #include<iostream> using namespace std; int bubble_counter=0,selection_counter=0; // variables global void bubble_sort(int [], int); void show_array(int [],int); void selectionsort(int [], int ); int main() { int a[7]={4,1,7,2,9,0,3}; show_array(a,7); //bubble_sort(a,7); selectionsort(a,7); show_array(a,7); cout<<"Bubble counter = "<<bubble_counter<<endl; cout<<"Selection Counter = "<<selection_counter<<endl; return 0; } void show_array(int array[],int size) { for( int i=0 ; i<size; i++) { cout<<array[i]<< " "; } cout<<endl; } void bubble_sort( int array[],...

  • How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the lin...

    How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the linear doesn't. Here's my code. #include <iostream> using namespace std; void selectionSort(int[], int, int& ); void showSelection(int[], int); void sortArray(int[], int, int&); void showArray(const int[], int); int main() {    int values[25] = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24...

  • Modify this C++ below to show the selection sorting algorithm method. Then write a test program...

    Modify this C++ below to show the selection sorting algorithm method. Then write a test program and show that it works. HINT: Replace the bubbleSort function with the selectionSort. Your test program should perform the following steps: Create an array of random numbers. Display the array in its original order. Pass the array to your sorting function. Display the array again to show that it has been sorted. Provide a screen shot showing that the above steps are working. CODE:...

  • Aim To improve the algorithm provided in Snippet 2.3 by reducing the number of passes. public...

    Aim To improve the algorithm provided in Snippet 2.3 by reducing the number of passes. public void sortImprovement1(int[] numbers) { for (int i = 1; i < numbers.length; i++) { for (int j = 0; j < numbers.length - i; j++) { if (numbers[j] > numbers[j + 1]) { swap(numbers, j, j + 1); } } } } Snippet 2.3: Bubble sort improvement 1 Change the bubble sort method so that it stops sorting if the array is untouched after...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • Hello this is my java sorting algorithm program i need all of my errors corrected so...

    Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner;    public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1)    bubbleSort(array); if (ch==2)    insertion(array); if (ch==3)    Selection(array); if (ch==4) break;    print(array);    System.out.println(); }    }...

  • I want to compare the runtimes and swap operations times among quick Sort, selection Sort and...

    I want to compare the runtimes and swap operations times among quick Sort, selection Sort and shell Sort here is my code: But when I create a 1000,000 size array, I can't get the result of the operations times and runtime. what's wrong with my code? and I also want to copy the array. Because I want to use same array for three sort. And for the shell Sort, I haven't learn it in my class. Can anyone help me...

  • Consider the following attempt at a selection sort algorithm implementation in Java: 1 public static void...

    Consider the following attempt at a selection sort algorithm implementation in Java: 1 public static void selectionSort(int[] a) { 2 int n = a.length; 3 for (int i = 0; i < n - 1; i++) { 4 int smallest = i; 5 for (int j = 1; j < n; j++) { 6 if (j < smallest) { 7 smallest = j; 8 } 9 } 10 if (i != smallest) { 11 int temp = a[smallest]; 12 a[smallest]...

  • //CODE 16-02.cpp //Demonstrates a template function that implements //a generic version of the selection sort algorithm....

    //CODE 16-02.cpp //Demonstrates a template function that implements //a generic version of the selection sort algorithm. #include <iostream> using std::cout; using std::endl; template<class T> void sort(T a[], int numberUsed); //Precondition: numberUsed <= declared size of the array a. //The array elements a[0] through a[numberUsed - 1] have values. //The assignment and < operator work for values of type T. //Postcondition: The values of a[0] through a[numberUsed - 1] have //been rearranged so that a[0] <= a[1] <=... <= a[numberUsed -...

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