?
Solved

my bubblesort for 100 ints

Posted on 2006-04-07
7
Medium Priority
?
802 Views
Last Modified: 2012-06-27
Hi I have been trying to implent this sorting algorithm for 100 ints.. I used the bubble sort one as well as quicksort...
I have spent nearly two weeks trying to code quicksort and gladly changed to bubble seen that qsort nearly gave me a nervous breakdown!

bubble sort is this:
Bubblesort (int data[],int n) {
   int tmp,i,j;

   for (i=0; i<n-1; i++) {
       for (j=0; j<n-i-1; j++)
            if (data[j] > data[j+1]) {
               tmp = data[j];
               data[j] = data[j+1];
               data[j+1] = tmp;
           }
   }
}



and here is my implementation in bubblesort........

      .data
array:      .word 0x11,0x111,0x1111111,0x11111,0x1,0x111111111,0x111111,0x111111111,0x111,0x11,0x1111,0x11111111,0x111,0x111111,0x111,0x11111111,0x111111111,0,0x1,0x1111111

      .text

      dadd r1, r0, r0  ;i
      dadd r2, r0, r0  ;j
      daddi r6, r2, 8  ;j+1
      ld r3, array(r0)  
      dsll r3, r3, 3  ;n
      daddi r7, r3, -8   ;n-1
label:      slt r12 ,r1, r7  ;check i< n-1
      beqz r12, done ;if not halt
      dsub r13, r3, r1  ; else set up for j loop....(ie) n-i
      daddi r14, r13, -8  ; n-i-1
here:      slt r15, r2, r14  ; if !r2 < n-i-1
      beqz r15,inci    ; go to next i
      ld r19, array(r2)  ; else do the swap
      ld r20, array(r6)
      slt r21, r19, r20
      bnez r21, incj
      ld r18, array(r6)
      ld r16, array(r2)
      sd r16, array(r6)
      sd r18, array(r2)
incj:      daddi r2, r2, 8   ; next iteration of j
      daddi r6, r2, 8
      j here
inci:      dadd r2, r0, r0  ; inc i and reset j and j+ 1
      daddi r1, r1, 8
      daddi r6, r2, 8
        j label ; go again
done:   halt




the thing is i have an application from that shows the stats of the  reg and mem etc.. a kind of simulator!   can be downloaded from here   http://www.computing.dcu.ie/~mike/winmips64.html   installs in seconds
but this sort is taking way to many clock cycles maybe it can be optimized better also it will not sort the last four of the array...please help
0
Comment
Question by:pablo23
  • 3
5 Comments
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16403132
Hi pablo23,

 >>  for (i=0; i<n-1; i++) {
You dont need to stop i at n-1 since j is stopping at n-i-1. Use:
   for (i=0; i<n; i++) {

I'll take a look at the rest in a bit.

Paul
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16403390
Hi pablo23,

I'm not familliar with this language so forgive me if these are not of use.

There's some optimisations you can do:

1. You use -8 and 8 as values. Have you got two more registers you can use to hold them?
2. You load both array elements into registers before comparison. Can you compare a register with a memory location?
3. ld r18, array(r6). Surely, that value is in r20 already.

Are there any instructions to read a number of values into sequential registers in one hit?

Paul
0
 

Author Comment

by:pablo23
ID: 16403612
No probs Paul-. Maybe I will try that with the r20 op (use a dadd)instead of ld, but the cc is still very poor and it won't sort the last five data regs.... and thats when its just sort 20 ints as you can see. Its up around 2000 and more! I want it down well past 1000 ;(!
0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 1000 total points
ID: 16403947
The problem of not sorting the last few should be covered by my first comment. Have you tried that?

You cannot judge the algorithms efficiency on one set of data. Bubblesort is least efficient if the data is initially in reverse order and most efficient if the data is already sorted. You must test with random data and average the results.

There will also be a minimum number of cycles for any sort, and that is to compare every item with at least one other. How many cycles would that take?

Paul
0
 
LVL 19

Assisted Solution

by:BrianGEFF719
BrianGEFF719 earned 1000 total points
ID: 16531791
Bubblesort is one of the worst sorting algorithms it has an order of magnitude of n^2 for large n.

If you have an ASM quicksort algorithm, I'd spend time working out the bugs in that as the order of magnitude for a properly written quick sort algorithm becomes closer to n log n.


Brian
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If anyone asked you to network diagram of the internet, it was drawn in the form of a fluffy cloud which further became known as cloud computing. Popularly cloud computing is defined as workloads that run over the internet in a commercial provider’s…
Strategic internal linking is often considered an SEO power technique, especially for content marketing. Do you need to hire an SEO agency to optimize you internal linking? No, this article will help you understand the basics of internal linking and…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question