Jabz

433 Reputation

5 Badges

12 years, 296 days

MaplePrimes Activity


These are replies submitted by Jabz

I finished coding the cocktail sort algorithm it works but it's a bit slow and it takes a long time to sort a list containing elements 30 or more.
 

 

restart:

cocktailsort:=proc(L)
local n,o,r,p,k,j,i:
 n:=rtable_dims(L):
 o:=rhs(n):
 r:=lhs(n):
   for p from o to r + 1 by -1 do
    for j from r to o - 1 do
    for k from o to r+1 by -1 do
    
      for i from r to o - 1 do
        if L[i] > L[i+1] then
        (L[i], L[i+1] ) := (L[i+1], L[i]);
       else
        (L[i], L[i+1] ) := (L[i], L[i+1] );
      fi:

    if L[k] < L[k-1] then
     (L[k-1],L[k]) := (L[k],L[k-1]);
    else
      (L[k-1],L[k]) := (L[k-1], L[k]);
    fi:
     od:
    od:
   od:
 od:
print(L);
end proc:


L:=rtable(1..5,random(1..100));
cocktailsort(L);

                                                             L:=[5 38 87 3 65]

 

                                                                [3 5 38 65 87]

 This version is a bit faster but am not sure if it is correct?

restart:

cocktailsort:=proc(L)
local n,o,r,k,i:
 n:=rtable_dims(L):
 o:=rhs(n):
 r:=lhs(n):
  

    for k from o to r+1 by -1 do
      for i from r to o - 1 do
        if L[i] > L[i+1] then
        (L[i], L[i+1] ) := (L[i+1], L[i]);
       else
        (L[i], L[i+1] ) := (L[i], L[i+1] );
      fi:

    if L[k] < L[k-1] then
     (L[k-1],L[k]) := (L[k],L[k-1]);
    else
      (L[k-1],L[k]) := (L[k-1], L[k]);
    fi:
     od:
    od:
 
print(L);
end proc:

L:=rtable(1..5,random(1..100));
cocktailsort(L);
 

                                                         L:=[5 38 87 3 65]

 

                                                                [3 5 38 65 87]

I finished coding the cocktail sort algorithm it works but it's a bit slow and it takes a long time to sort a list containing elements 30 or more.
 

 

restart:

cocktailsort:=proc(L)
local n,o,r,p,k,j,i:
 n:=rtable_dims(L):
 o:=rhs(n):
 r:=lhs(n):
   for p from o to r + 1 by -1 do
    for j from r to o - 1 do
    for k from o to r+1 by -1 do
    
      for i from r to o - 1 do
        if L[i] > L[i+1] then
        (L[i], L[i+1] ) := (L[i+1], L[i]);
       else
        (L[i], L[i+1] ) := (L[i], L[i+1] );
      fi:

    if L[k] < L[k-1] then
     (L[k-1],L[k]) := (L[k],L[k-1]);
    else
      (L[k-1],L[k]) := (L[k-1], L[k]);
    fi:
     od:
    od:
   od:
 od:
print(L);
end proc:


L:=rtable(1..5,random(1..100));
cocktailsort(L);

                                                             L:=[5 38 87 3 65]

 

                                                                [3 5 38 65 87]

 This version is a bit faster but am not sure if it is correct?

restart:

cocktailsort:=proc(L)
local n,o,r,k,i:
 n:=rtable_dims(L):
 o:=rhs(n):
 r:=lhs(n):
  

    for k from o to r+1 by -1 do
      for i from r to o - 1 do
        if L[i] > L[i+1] then
        (L[i], L[i+1] ) := (L[i+1], L[i]);
       else
        (L[i], L[i+1] ) := (L[i], L[i+1] );
      fi:

    if L[k] < L[k-1] then
     (L[k-1],L[k]) := (L[k],L[k-1]);
    else
      (L[k-1],L[k]) := (L[k-1], L[k]);
    fi:
     od:
    od:
 
print(L);
end proc:

L:=rtable(1..5,random(1..100));
cocktailsort(L);
 

                                                         L:=[5 38 87 3 65]

 

                                                                [3 5 38 65 87]

oh my days something like that should be common sense it seems so simple now lol. I am VERY grateful for ur kind help thank you. It's working now. I'm going to attemp to put both methods into one procedure so it forms cocktail sort. Thanks.

 

p.s if i was to split a list using 'lenghtsplit' how do i merge the split lists into one list again. I looked at maple help but the closest command i found was 'zip' but it dosen't really attach 2 list into 1. I havn't tried spliting an array yet am not even sure if that's possible but if it is then how am i going to rejoin the array?

oh my days something like that should be common sense it seems so simple now lol. I am VERY grateful for ur kind help thank you. It's working now. I'm going to attemp to put both methods into one procedure so it forms cocktail sort. Thanks.

 

p.s if i was to split a list using 'lenghtsplit' how do i merge the split lists into one list again. I looked at maple help but the closest command i found was 'zip' but it dosen't really attach 2 list into 1. I havn't tried spliting an array yet am not even sure if that's possible but if it is then how am i going to rejoin the array?

I tried that but i wasnt very succesful. I kept on getting an error ' Error, Array index out of range' .I'll show u what i got.

 

restart:
L:=rtable(1..6,random(1..10));
n:=rtable_dims(L);
o:=rhs(n);
r:=lhs(n);

for j from o to r by -1 do
for i from o to r by -1 do

if L[i] < L[i-1] then
 (L[i-1],L[i]) := (L[i],L[i-1]);
else
(L[i-1],L[i]) := (L[i-1], L[i]);
fi:od: od:
print(L);
                           L:=[4 5 8 7 3 5]
                                  n:= 1 .. 6
                                    0:=  6
                                     r:= 1
Error, Array index out of range
                            [3 4 5 8 7 5]
 

I tried that but i wasnt very succesful. I kept on getting an error ' Error, Array index out of range' .I'll show u what i got.

 

restart:
L:=rtable(1..6,random(1..10));
n:=rtable_dims(L);
o:=rhs(n);
r:=lhs(n);

for j from o to r by -1 do
for i from o to r by -1 do

if L[i] < L[i-1] then
 (L[i-1],L[i]) := (L[i],L[i-1]);
else
(L[i-1],L[i]) := (L[i-1], L[i]);
fi:od: od:
print(L);
                           L:=[4 5 8 7 3 5]
                                  n:= 1 .. 6
                                    0:=  6
                                     r:= 1
Error, Array index out of range
                            [3 4 5 8 7 5]
 

the reason why i used 'conver(%,'list') is because without it 'nops(L)' returns 4 but there is 6 elements in the rtable not 4. The only way i could get it to read the correct elements is by converting the rtable into a list. Oh n i see wot u mean i didnt know the default step size was + 1 i presumed it would decrease automatically. So silly of me lol thank u.

the reason why i used 'conver(%,'list') is because without it 'nops(L)' returns 4 but there is 6 elements in the rtable not 4. The only way i could get it to read the correct elements is by converting the rtable into a list. Oh n i see wot u mean i didnt know the default step size was + 1 i presumed it would decrease automatically. So silly of me lol thank u.

i am very gratefull for ur assistance thank u.

 

i am very gratefull for ur assistance thank u.

 

Thank u. Thats going to be really helpful when i code the Merge sort algorithm. i hav already coded half of it. i will post it in here after i completed it.

can u assist me in codeing the cocktail sort algorithm. i can't seem to code it going backwards in a given list or array?

Thank u. Thats going to be really helpful when i code the Merge sort algorithm. i hav already coded half of it. i will post it in here after i completed it.

can u assist me in codeing the cocktail sort algorithm. i can't seem to code it going backwards in a given list or array?

Thank You for the explanation. I was expecting a 1-4 line code but you went through the trouble of explaing the whole process in detail thank you very much i really appriciate it and it was really benefial for me because i understood every step Thank again.

Thank You for the explanation. I was expecting a 1-4 line code but you went through the trouble of explaing the whole process in detail thank you very much i really appriciate it and it was really benefial for me because i understood every step Thank again.

Thank you for the explanation. So i would get a more accurate time if i use 'time()' instead of 'time[real]'. I'll try just time and see what happens. Thanks again

3 4 5 6 7 8 9 Page 5 of 9