Jump to content

Help with sorting algorithm in javascript

Go to solution Solved by RushFan,

It was just because java(script?) references objects when you make them equal to another one so I just made a new temporary one. 

 

NEW CODE:

 

tri[] fintris = new tri[calctris.length];
  for(int i = 0; i < fintris.length; i++){
    int maxCnt = 0;
    float maxVal = 0;
    for(int i1 = 0; i1 < calctris.length; i1++){
      if(maxVal < calctris[i1].a.z){
        maxCnt = i1;
        maxVal = calctris[i1].a.z;
        fintris[ i] = new tri(new vert(calctris[i1].a.x, calctris[i1].a.y, calctris[i1].a.z), new vert(calctris[i1].b.x, calctris[i1].b.y, calctris[i1].a.z), new vert(calctris[i1].c.x, calctris[i1].c.y, calctris[i1].a.z)); 
      }
    }
    calctris[maxCnt].a.z = 0;
  }

Hi, so I'm trying to make a sorting algorithm which copies one array into another but sorts it from highest to lowest. I don't understand online examples very much so I thought I'd ask here. (also I'm not a very experienced programmer so my code is probably going to be messy and probably not very good)

 

I have a class called tri(for storing triangle data) and I'm sorting it based on its '.a.z' value. 

 

(calctris[] has already been made)

 

the code for the (not working) current one is:

tri[] fintris = new tri[calctris.length];
  for(int i = 0; i < calctris.length; i++){
    int maxCnt = 0;
    float maxVal = 0;
    for(int i1 = 0; i1 < calctris.length; i1++){
      if(maxVal < calctris[i1].a.z){
        maxCnt = i1;
        maxVal = calctris[i1].a.z;
        fintris = calctris[i1]; 
      }
    }
    calctris[maxCnt].a.z = 0;
  }

 

it's just filling the new array with 0.0;

-Parts-

Core i7 8086k 4.0GHz (4.6GHz OC) - ASUS z390 ROG Maximus XI code - Corsair H115i - 16GB (2 x 8GB) G.SKILL Trident Z RGB 3600MHz - Windforce GTX 1050Ti - Corsair HX750i - Corsair Obsidian 500D RGB SE

 

-Upgrades when I get the money-

Undecided on the GPU - ASUS ROG PG278QR 1440p 144Hz (165Hz OC)

Link to comment
https://linustechtips.com/topic/1041806-help-with-sorting-algorithm-in-javascript/
Share on other sites

Link to post
Share on other sites

While I shouldn't be helping you with your CS homework here's some bubble sort code I made. Hopefully it should help you as I'm not going to solve it for you. You gotta learn ;)

 

#include-once
#include <AutoItConstants.au3>

; #FUNCTION# ====================================================================================================================
; Name ..........: BubbleSort
; Description ...: Sorts a 1 or 2 dimensional array using the BubbleSort Algorithm
; Syntax ........: BubbleSort($_aArray[, $_bAscending = True])
; Parameters ....: $_aArray             - [in/out] The 1 dimensional array to sort.
;                  $_bAscending         - [optional] Sort by increasing values. Default is True.
; Return values .: Success - Returns 1
;                  Failure - Returns 0 and sets @error:
;                  |1 = Invalid Array, sets @extented: (1, if not a valid array; 2, if not 1 or 2 dimesional)
;                  |2 = Invalid $_bAscending Flag
; Author ........: Robert C. Maehl (rcmaehl)
; Modified ......: 11/30/2015
; Remarks .......: To do: Complete Two Dimesional Array sorting ([first,first] = lowest, [last,last] = highest)
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func BubbleSort(ByRef $_aArray, $_bAscending = True)
    Local $_vReturn = Null
    Local $_bFinished = False
    If Not IsArray($_aArray) Then SetError(1,1,0)
    If UBound($_aArray, $UBOUND_DIMENSIONS) > 2 Then SetError(1,2,0)
    If Not IsBool($_bAscending) Then SetError(2,0,0)
    If $_vReturn = Null Then
        If UBound($_aArray, $UBOUND_DIMENSIONS) = 1 Then
            While Not $_bFinished
                $_bFinished = True
                $_iLoop = 0
                $_vTemp = 0
                If $_bAscending Then
                    For $_iLoop = 0 To UBound($_aArray) - 2
                        If $_aArray[$_iLoop] > $_aArray[$_iLoop + 1] Then
                            $_vTemp = $_aArray[$_iLoop]
                            $_aArray[$_iLoop] = $_aArray[$_iLoop + 1]
                            $_aArray[$_iLoop + 1] = $_vTemp
                            $_bFinished = False
                        EndIf
                    Next
                Else
                    For $_iLoop = 0 To UBound($_aArray) - 2
                        If $_aArray[$_iLoop] < $_aArray[$_iLoop + 1] Then
                            $_vTemp = $_aArray[$_iLoop]
                            $_aArray[$_iLoop] = $_aArray[$_iLoop + 1]
                            $_aArray[$_iLoop + 1] = $_vTemp
                            $_bFinished = False
                        EndIf
                    Next
                EndIf
            WEnd
        Else ; 2 Dimesional Sorting, Not Yet Complete. (Sort all rows & columns with [0,0] with smallest, and [last,last] with largest)
            $_iLoop1 = 0
            For $_iLoop1 = 0 To UBound($_aArray, $UBOUND_ROWS) - 1
                $_bFinished = False
                While Not $_bFinished
                    $_bFinished = True
                    $_iLoop2 = 0
                    $_vTemp = 0
                    If $_bAscending Then
                        For $_iLoop2 = 0 To UBound($_aArray, $UBOUND_COLUMNS) - 2
                            If $_aArray[$_iLoop1][$_iLoop2] > $_aArray[$_iLoop1][$_iLoop2 + 1] Then
                                $_vTemp = $_aArray[$_iLoop1][$_iLoop2]
                                $_aArray[$_iLoop1][$_iLoop2] = $_aArray[$_iLoop1][$_iLoop2 + 1]
                                $_aArray[$_iLoop1][$_iLoop2 + 1] = $_vTemp
                                $_bFinished = False
                            EndIf
                        Next
                    Else
                        For $_iLoop2 = 0 To UBound($_aArray, $UBOUND_COLUMNS) - 2
                            If $_aArray[$_iLoop1][$_iLoop2] < $_aArray[$_iLoop1][$_iLoop2 + 1] Then
                                $_vTemp = $_aArray[$_iLoop1][$_iLoop2]
                                $_aArray[$_iLoop1][$_iLoop2] = $_aArray[$_iLoop1][$_iLoop2 + 1]
                                $_aArray[$_iLoop1][$_iLoop2 + 1] = $_vTemp
                                $_bFinished = False
                            EndIf
                        Next
                    EndIf
                WEnd
            Next
        EndIf
    EndIf
EndFunc

 

PLEASE QUOTE ME IF YOU ARE REPLYING TO ME

Desktop Build: Ryzen 7 2700X @ 4.0GHz, AsRock Fatal1ty X370 Professional Gaming, 48GB Corsair DDR4 @ 3000MHz, RX5700 XT 8GB Sapphire Nitro+, Benq XL2730 1440p 144Hz FS

42U Server Rack: ISP Modem + UDM-SE + APC 3kVA UPS + 3x Dell Precision 5820 + TBD

Retro Build: Intel Pentium III @ 500 MHz, Dell Optiplex G1 Full AT Tower, 768MB SDRAM @ 133MHz, Integrated Graphics, Generic 1024x768 60Hz Monitor


 

Link to post
Share on other sites

4 minutes ago, rcmaehl said:

While I shouldn't be helping you with your CS homework here's some bubble sort code I made. Hopefully it should help you as I'm not going to solve it for you. You gotta learn ;)

 


#include-once
#include <AutoItConstants.au3>

; #FUNCTION# ====================================================================================================================
; Name ..........: BubbleSort
; Description ...: Sorts a 1 or 2 dimensional array using the BubbleSort Algorithm
; Syntax ........: BubbleSort($_aArray[, $_bAscending = True])
; Parameters ....: $_aArray             - [in/out] The 1 dimensional array to sort.
;                  $_bAscending         - [optional] Sort by increasing values. Default is True.
; Return values .: Success - Returns 1
;                  Failure - Returns 0 and sets @error:
;                  |1 = Invalid Array, sets @extented: (1, if not a valid array; 2, if not 1 or 2 dimesional)
;                  |2 = Invalid $_bAscending Flag
; Author ........: Robert C. Maehl (rcmaehl)
; Modified ......: 11/30/2015
; Remarks .......: To do: Complete Two Dimesional Array sorting ([first,first] = lowest, [last,last] = highest)
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func BubbleSort(ByRef $_aArray, $_bAscending = True)
    Local $_vReturn = Null
    Local $_bFinished = False
    If Not IsArray($_aArray) Then SetError(1,1,0)
    If UBound($_aArray, $UBOUND_DIMENSIONS) > 2 Then SetError(1,2,0)
    If Not IsBool($_bAscending) Then SetError(2,0,0)
    If $_vReturn = Null Then
        If UBound($_aArray, $UBOUND_DIMENSIONS) = 1 Then
            While Not $_bFinished
                $_bFinished = True
                $_iLoop = 0
                $_vTemp = 0
                If $_bAscending Then
                    For $_iLoop = 0 To UBound($_aArray) - 2
                        If $_aArray[$_iLoop] > $_aArray[$_iLoop + 1] Then
                            $_vTemp = $_aArray[$_iLoop]
                            $_aArray[$_iLoop] = $_aArray[$_iLoop + 1]
                            $_aArray[$_iLoop + 1] = $_vTemp
                            $_bFinished = False
                        EndIf
                    Next
                Else
                    For $_iLoop = 0 To UBound($_aArray) - 2
                        If $_aArray[$_iLoop] < $_aArray[$_iLoop + 1] Then
                            $_vTemp = $_aArray[$_iLoop]
                            $_aArray[$_iLoop] = $_aArray[$_iLoop + 1]
                            $_aArray[$_iLoop + 1] = $_vTemp
                            $_bFinished = False
                        EndIf
                    Next
                EndIf
            WEnd
        Else ; 2 Dimesional Sorting, Not Yet Complete. (Sort all rows & columns with [0,0] with smallest, and [last,last] with largest)
            $_iLoop1 = 0
            For $_iLoop1 = 0 To UBound($_aArray, $UBOUND_ROWS) - 1
                $_bFinished = False
                While Not $_bFinished
                    $_bFinished = True
                    $_iLoop2 = 0
                    $_vTemp = 0
                    If $_bAscending Then
                        For $_iLoop2 = 0 To UBound($_aArray, $UBOUND_COLUMNS) - 2
                            If $_aArray[$_iLoop1][$_iLoop2] > $_aArray[$_iLoop1][$_iLoop2 + 1] Then
                                $_vTemp = $_aArray[$_iLoop1][$_iLoop2]
                                $_aArray[$_iLoop1][$_iLoop2] = $_aArray[$_iLoop1][$_iLoop2 + 1]
                                $_aArray[$_iLoop1][$_iLoop2 + 1] = $_vTemp
                                $_bFinished = False
                            EndIf
                        Next
                    Else
                        For $_iLoop2 = 0 To UBound($_aArray, $UBOUND_COLUMNS) - 2
                            If $_aArray[$_iLoop1][$_iLoop2] < $_aArray[$_iLoop1][$_iLoop2 + 1] Then
                                $_vTemp = $_aArray[$_iLoop1][$_iLoop2]
                                $_aArray[$_iLoop1][$_iLoop2] = $_aArray[$_iLoop1][$_iLoop2 + 1]
                                $_aArray[$_iLoop1][$_iLoop2 + 1] = $_vTemp
                                $_bFinished = False
                            EndIf
                        Next
                    EndIf
                WEnd
            Next
        EndIf
    EndIf
EndFunc

 

I was more hoping for someone to explain what was wrong with the one I made, but thanks

(also this isn't homework, just part of a side project that I'm having fun with)

-Parts-

Core i7 8086k 4.0GHz (4.6GHz OC) - ASUS z390 ROG Maximus XI code - Corsair H115i - 16GB (2 x 8GB) G.SKILL Trident Z RGB 3600MHz - Windforce GTX 1050Ti - Corsair HX750i - Corsair Obsidian 500D RGB SE

 

-Upgrades when I get the money-

Undecided on the GPU - ASUS ROG PG278QR 1440p 144Hz (165Hz OC)

Link to post
Share on other sites

11 minutes ago, MrMG said:

Are you sure you are coding with Javascript? I don't know much about Javascript but your code looks more like Java code to me.

Idk honestly, I'm using processing and I thought it was javascript

-Parts-

Core i7 8086k 4.0GHz (4.6GHz OC) - ASUS z390 ROG Maximus XI code - Corsair H115i - 16GB (2 x 8GB) G.SKILL Trident Z RGB 3600MHz - Windforce GTX 1050Ti - Corsair HX750i - Corsair Obsidian 500D RGB SE

 

-Upgrades when I get the money-

Undecided on the GPU - ASUS ROG PG278QR 1440p 144Hz (165Hz OC)

Link to post
Share on other sites

I have no idea what processing is but according to wikipedia it has a Java like syntax, but uses Javascript for rendering on webpages.

 

I have looked at your code and I have found one thing that might be wrong:

fintris = calctris[i1]; 

This line inside your second for loop, might be wrong. As far as I know fintris is an array and that line should look something like:

fintris[i1] = calctris[i1]; 

Not sure if that solves your problem though.

Link to post
Share on other sites

1 minute ago, MrMG said:

I have no idea what processing is but according to wikipedia it has a Java like syntax, but uses Javascript for rendering on webpages.

 

I have looked at your code and I have found one thing that might be wrong:


fintris = calctris[i1]; 

This line inside your second for loop, might be wrong. As far as I know fintris is an array and that line should look something like:


fintris[i1] = calctris[i1]; 

Not sure if that solves your problem though.

That was an issue, but I found the problem and it was something else

 

-Parts-

Core i7 8086k 4.0GHz (4.6GHz OC) - ASUS z390 ROG Maximus XI code - Corsair H115i - 16GB (2 x 8GB) G.SKILL Trident Z RGB 3600MHz - Windforce GTX 1050Ti - Corsair HX750i - Corsair Obsidian 500D RGB SE

 

-Upgrades when I get the money-

Undecided on the GPU - ASUS ROG PG278QR 1440p 144Hz (165Hz OC)

Link to post
Share on other sites

It was just because java(script?) references objects when you make them equal to another one so I just made a new temporary one. 

 

NEW CODE:

 

tri[] fintris = new tri[calctris.length];
  for(int i = 0; i < fintris.length; i++){
    int maxCnt = 0;
    float maxVal = 0;
    for(int i1 = 0; i1 < calctris.length; i1++){
      if(maxVal < calctris[i1].a.z){
        maxCnt = i1;
        maxVal = calctris[i1].a.z;
        fintris[ i] = new tri(new vert(calctris[i1].a.x, calctris[i1].a.y, calctris[i1].a.z), new vert(calctris[i1].b.x, calctris[i1].b.y, calctris[i1].a.z), new vert(calctris[i1].c.x, calctris[i1].c.y, calctris[i1].a.z)); 
      }
    }
    calctris[maxCnt].a.z = 0;
  }

-Parts-

Core i7 8086k 4.0GHz (4.6GHz OC) - ASUS z390 ROG Maximus XI code - Corsair H115i - 16GB (2 x 8GB) G.SKILL Trident Z RGB 3600MHz - Windforce GTX 1050Ti - Corsair HX750i - Corsair Obsidian 500D RGB SE

 

-Upgrades when I get the money-

Undecided on the GPU - ASUS ROG PG278QR 1440p 144Hz (165Hz OC)

Link to post
Share on other sites

Just now, RushFan said:

It was just because java(script?) references objects when you make them equal to another one so I just made a new temporary one. 

 

NEW CODE:

 

tri[] fintris = new tri[calctris.length];
  for(int i = 0; i < fintris.length; i++){
    int maxCnt = 0;
    float maxVal = 0;
    for(int i1 = 0; i1 < calctris.length; i1++){
      if(maxVal < calctris[i1].a.z){
        maxCnt = i1;
        maxVal = calctris[i1].a.z;
        fintris = new tri(new vert(calctris[i1].a.x, calctris[i1].a.y, calctris[i1].a.z), new vert(calctris[i1].b.x, calctris[i1].b.y, calctris[i1].a.z), new vert(calctris[i1].c.x, calctris[i1].c.y, calctris[i1].a.z)); 
      }
    }
    calctris[maxCnt].a.z = 0;
  }

Ik that can be done better but shhhh alright ik what I need to do

-Parts-

Core i7 8086k 4.0GHz (4.6GHz OC) - ASUS z390 ROG Maximus XI code - Corsair H115i - 16GB (2 x 8GB) G.SKILL Trident Z RGB 3600MHz - Windforce GTX 1050Ti - Corsair HX750i - Corsair Obsidian 500D RGB SE

 

-Upgrades when I get the money-

Undecided on the GPU - ASUS ROG PG278QR 1440p 144Hz (165Hz OC)

Link to post
Share on other sites

2 minutes ago, RushFan said:

It was just because java(script?) references objects when you make them equal to another one so I just made a new temporary one. 

 

NEW CODE:

 

tri[] fintris = new tri[calctris.length];
  for(int i = 0; i < fintris.length; i++){
    int maxCnt = 0;
    float maxVal = 0;
    for(int i1 = 0; i1 < calctris.length; i1++){
      if(maxVal < calctris[i1].a.z){
        maxCnt = i1;
        maxVal = calctris[i1].a.z;
        fintris = new tri(new vert(calctris[i1].a.x, calctris[i1].a.y, calctris[i1].a.z), new vert(calctris[i1].b.x, calctris[i1].b.y, calctris[i1].a.z), new vert(calctris[i1].c.x, calctris[i1].c.y, calctris[i1].a.z)); 
      }
    }
    calctris[maxCnt].a.z = 0;
  }

also there is an [i ]  after fintris but this forum thinks it's me trying to make it italic

-Parts-

Core i7 8086k 4.0GHz (4.6GHz OC) - ASUS z390 ROG Maximus XI code - Corsair H115i - 16GB (2 x 8GB) G.SKILL Trident Z RGB 3600MHz - Windforce GTX 1050Ti - Corsair HX750i - Corsair Obsidian 500D RGB SE

 

-Upgrades when I get the money-

Undecided on the GPU - ASUS ROG PG278QR 1440p 144Hz (165Hz OC)

Link to post
Share on other sites

Just now, MrMG said:

Great to hear that you found the solution. If you want to add code to your posts, then consider using the code tags:

image.png.7e5b1131361b3a0d859188152934ac15.png

That way everything should be copied correctly.

Ok, I'll keep that in mind if I have any more questions about my code

-Parts-

Core i7 8086k 4.0GHz (4.6GHz OC) - ASUS z390 ROG Maximus XI code - Corsair H115i - 16GB (2 x 8GB) G.SKILL Trident Z RGB 3600MHz - Windforce GTX 1050Ti - Corsair HX750i - Corsair Obsidian 500D RGB SE

 

-Upgrades when I get the money-

Undecided on the GPU - ASUS ROG PG278QR 1440p 144Hz (165Hz OC)

Link to post
Share on other sites

14 hours ago, rcmaehl said:

While I shouldn't be helping you with your CS homework here's some bubble sort code I made. Hopefully it should help you as I'm not going to solve it for you. You gotta learn ;)

 


#include-once
#include <AutoItConstants.au3>

; #FUNCTION# ====================================================================================================================
; Name ..........: BubbleSort
; Description ...: Sorts a 1 or 2 dimensional array using the BubbleSort Algorithm
; Syntax ........: BubbleSort($_aArray[, $_bAscending = True])
; Parameters ....: $_aArray             - [in/out] The 1 dimensional array to sort.
;                  $_bAscending         - [optional] Sort by increasing values. Default is True.
; Return values .: Success - Returns 1
;                  Failure - Returns 0 and sets @error:
;                  |1 = Invalid Array, sets @extented: (1, if not a valid array; 2, if not 1 or 2 dimesional)
;                  |2 = Invalid $_bAscending Flag
; Author ........: Robert C. Maehl (rcmaehl)
; Modified ......: 11/30/2015
; Remarks .......: To do: Complete Two Dimesional Array sorting ([first,first] = lowest, [last,last] = highest)
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func BubbleSort(ByRef $_aArray, $_bAscending = True)
    Local $_vReturn = Null
    Local $_bFinished = False
    If Not IsArray($_aArray) Then SetError(1,1,0)
    If UBound($_aArray, $UBOUND_DIMENSIONS) > 2 Then SetError(1,2,0)
    If Not IsBool($_bAscending) Then SetError(2,0,0)
    If $_vReturn = Null Then
        If UBound($_aArray, $UBOUND_DIMENSIONS) = 1 Then
            While Not $_bFinished
                $_bFinished = True
                $_iLoop = 0
                $_vTemp = 0
                If $_bAscending Then
                    For $_iLoop = 0 To UBound($_aArray) - 2
                        If $_aArray[$_iLoop] > $_aArray[$_iLoop + 1] Then
                            $_vTemp = $_aArray[$_iLoop]
                            $_aArray[$_iLoop] = $_aArray[$_iLoop + 1]
                            $_aArray[$_iLoop + 1] = $_vTemp
                            $_bFinished = False
                        EndIf
                    Next
                Else
                    For $_iLoop = 0 To UBound($_aArray) - 2
                        If $_aArray[$_iLoop] < $_aArray[$_iLoop + 1] Then
                            $_vTemp = $_aArray[$_iLoop]
                            $_aArray[$_iLoop] = $_aArray[$_iLoop + 1]
                            $_aArray[$_iLoop + 1] = $_vTemp
                            $_bFinished = False
                        EndIf
                    Next
                EndIf
            WEnd
        Else ; 2 Dimesional Sorting, Not Yet Complete. (Sort all rows & columns with [0,0] with smallest, and [last,last] with largest)
            $_iLoop1 = 0
            For $_iLoop1 = 0 To UBound($_aArray, $UBOUND_ROWS) - 1
                $_bFinished = False
                While Not $_bFinished
                    $_bFinished = True
                    $_iLoop2 = 0
                    $_vTemp = 0
                    If $_bAscending Then
                        For $_iLoop2 = 0 To UBound($_aArray, $UBOUND_COLUMNS) - 2
                            If $_aArray[$_iLoop1][$_iLoop2] > $_aArray[$_iLoop1][$_iLoop2 + 1] Then
                                $_vTemp = $_aArray[$_iLoop1][$_iLoop2]
                                $_aArray[$_iLoop1][$_iLoop2] = $_aArray[$_iLoop1][$_iLoop2 + 1]
                                $_aArray[$_iLoop1][$_iLoop2 + 1] = $_vTemp
                                $_bFinished = False
                            EndIf
                        Next
                    Else
                        For $_iLoop2 = 0 To UBound($_aArray, $UBOUND_COLUMNS) - 2
                            If $_aArray[$_iLoop1][$_iLoop2] < $_aArray[$_iLoop1][$_iLoop2 + 1] Then
                                $_vTemp = $_aArray[$_iLoop1][$_iLoop2]
                                $_aArray[$_iLoop1][$_iLoop2] = $_aArray[$_iLoop1][$_iLoop2 + 1]
                                $_aArray[$_iLoop1][$_iLoop2 + 1] = $_vTemp
                                $_bFinished = False
                            EndIf
                        Next
                    EndIf
                WEnd
            Next
        EndIf
    EndIf
EndFunc

 

What in the heck is AutoIT? I had to google the .au3 file extension. This looks like Visual Basic but even more atrocious. I'm shook.

Current PC build: [CPU: Intel i7 8700k] [GPU: GTX 1070 Asus ROG Strix] [Ram: Corsair LPX 32GB 3000MHz] [Mobo: Asus Prime Z370-A] [SSD: Samsung 970 EVO 500GB primary + Samsung 860 Evo 1TB secondary] [PSU: EVGA SuperNova G2 750w 80plus] [Monitors: Dual Dell Ultrasharp U2718Qs, 4k IPS] [Case: Fractal Design R5]

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×