Question

using MIPS MARS sort an array of n integers and print both the sorted and unsorted...

using MIPS MARS sort an array of n integers and print both the sorted and unsorted array. n should be greater than or equal to 10 and entered by the user. Reminder make sure to use MIPS MARS only

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

// Sort elements by frequency. If two elements have same
// count, then put the elements that appears first
#include<bits/stdc++.h>
using namespace std;

// Used for sorting
struct ele
{
   int count, index, val;
};

// Used for sorting by value
bool mycomp(struct ele a, struct ele b) {
   return (a.val < b.val);
}

// Used for sorting by frequency. And if frequency is same,
// then by appearance
bool mycomp2(struct ele a, struct ele b) {
   if (a.count != b.count) return (a.count < b.count);
   else return a.index > b.index;
}

void sortByFrequency(int arr[], int n)
{
   struct ele element[n];
   for (int i = 0; i < n; i++)
   {
       element[i].index = i; /* Fill Indexes */
       element[i].count = 0; /* Initialize counts as 0 */
       element[i].val = arr[i]; /* Fill values in structure
                                   elements */
   }

   /* Sort the structure elements according to value,
   we used stable sort so relative order is maintained. */
   stable_sort(element, element+n, mycomp);

   /* initialize count of first element as 1 */
   element[0].count = 1;

   /* Count occurrences of remaining elements */
   for (int i = 1; i < n; i++)
   {
       if (element[i].val == element[i-1].val)
       {
           element[i].count += element[i-1].count+1;

           /* Set count of previous element as -1 , we are
           doing this because we'll again sort on the
           basis of counts (if counts are equal than on
           the basis of index)*/
           element[i-1].count = -1;

           /* Retain the first index (Remember first index
           is always present in the first duplicate we
           used stable sort. */
           element[i].index = element[i-1].index;
       }

       /* Else If previous element is not equal to current
       so set the count to 1 */
       else element[i].count = 1;
   }

   /* Now we have counts and first index for each element so now
   sort on the basis of count and in case of tie use index
   to sort.*/
   stable_sort(element, element+n, mycomp2);
   for (int i = n-1, index=0; i >= 0; i--)
       if (element[i].count != -1)
       for (int j=0; j<element[i].count; j++)
               arr[index++] = element[i].val;
}

// Driver program
int main()
{
   int arr[] = {2, 5, 2, 6, -1, 9999999, 5, 8, 8, 8};
   int n = sizeof(arr)/sizeof(arr[0]);

   sortByFrequency(arr, n);

   for (int i=0; i<n; i++)
   cout << arr[i] << " ";
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
using MIPS MARS sort an array of n integers and print both the sorted and unsorted...
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
  • Transfer C code of selection sort to MIPS code and print the sorted array/results data Array:...

    Transfer C code of selection sort to MIPS code and print the sorted array/results data Array: word 43, -5, 11, 12, 64, -7, 14, 71, 70, 13, -27 string: asciz"In" # Trantec the C code of selection sort to MIPS code. Do not modify the existing code and structure! text main la ŞtO, Array li $t1, 0 li $t7,11 mul $17, $17, 4 subi $t8,$t7, 4 # array length n-11 # 4*n #4*(n-1) # lis in $t1 and j is...

  • Transfer C code of selection sort to MIPS code and print the sorted array/results

    Transfer C code of selection sort to MIPS code and print the sorted array/results data Array: word 43, -5, 11, 12, 64, -7, 14, 71, 70, 13, -27 string: asciz"In" # Trantec the C code of selection sort to MIPS code. Do not modify the existing code and structure! text main la ŞtO, Array li $t1, 0 li $t7,11 mul $17, $17, 4 subi $t8,$t7, 4 # array length n-11 # 4*n #4*(n-1) # lis in $t1 and j is...

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

  • Hi, I am having trouble with the following question: Given an unsorted array with integers, find...

    Hi, I am having trouble with the following question: Given an unsorted array with integers, find the median of it using the Quick Select method we’ve discussed in the class (Hint: We use the quick select to find the kth smallest element in an unsorted array in O(n) time complexity). Note: A median is the middle number of the array after it is sorted. And in this problem, we return the N/2-th number after sorted if there are even numbers...

  • MIPS MIPS MIPS PLEASE INCLUDE COMMENTS AND OUTPUT Sort array using Bubble sort algorithm. 1) First...

    MIPS MIPS MIPS PLEASE INCLUDE COMMENTS AND OUTPUT Sort array using Bubble sort algorithm. 1) First ask the user how many elements of his/her array. 2) Then, read the integer array elements as input from the User. 3) Then, print out the array before the sorting 4) Apply Bubble sort algorithm on your array 5) Print out the array after the sorting 6) Print some welcome text to th user 7) Add comments to your code to describe how is...

  • Write a MIPS assembly language for sorting an array of integers using non-recursive bottom-up merge sort...

    Write a MIPS assembly language for sorting an array of integers using non-recursive bottom-up merge sort algorithm. Your program should print the processed array after each step of the merge sort. For example, if the input array is 14 27 13 11 49 63 17 9, your program should print each sort process: Input Arra;y 14 27 13 11 49 63 17 9 Print After first Iteration 14 27 11 13 49 639 17 Print After second iteration 11 13...

  • Radix sort C++ Come up with an unsorted array of numbers (integer array). Sort the numbers...

    Radix sort C++ Come up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. USE THIS STUCTURE struct nodeQ{                            node *front;                            node *rear;               };               nodeQ que[10]; enqueue(que[i],data); EXAMPLE: Original, unsorted list: [170, 45, 75, 90, 2, 802, 24, 66] 1ST PASS:...

  • MIPS insertion sort program......could really use some assistance

    Write a program in MIPS assembly language that implements the DESCENDING insertion 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...

  • MIPS insertion sort program......could really use some assistance

    Write a program in MIPS assembly language that implements the DESCENDING insertion 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...

  • In Java: In a sorted (ascending) integer array of length n with no duplicates, print all...

    In Java: In a sorted (ascending) integer array of length n with no duplicates, print all values in the range x to y. Assume both x and y are in the array. What is the worst case big O running time if there are k integers within the range?

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