File tree Expand file tree Collapse file tree 2 files changed +40
-1
lines changed
Expand file tree Collapse file tree 2 files changed +40
-1
lines changed Original file line number Diff line number Diff line change @@ -39,4 +39,6 @@ function divideNoPivo(array) {
3939return array ;
4040}
4141
42- console . log ( divideNoPivo ( listaDeLivros ) ) ;
42+ //console.log(divideNoPivo(listaDeLivros));
43+
44+ module . exports = trocaLugar ;
Original file line number Diff line number Diff line change 1+ const listaLivros = require ( "./lista" ) ;
2+ const trocaLugar = require ( './encontraMenores' )
3+
4+ function quickSort ( array , left , right ) {
5+ if ( array . length > 1 ) {
6+ let indiceAtual = particiona ( array , left , right ) ;
7+ if ( left < indiceAtual - 1 ) {
8+ quickSort ( array , left , indiceAtual - 1 ) ;
9+ }
10+ if ( indiceAtual < right ) {
11+ quickSort ( array , indiceAtual , right )
12+ }
13+ }
14+ return array ;
15+ }
16+
17+ function particiona ( array , left , right ) {
18+ let pivo = array [ Math . floor ( ( left + right ) / 2 ) ] ;
19+ let atualLeft = left ; //0
20+ let atualRight = right ; //10
21+
22+ while ( atualLeft <= atualRight ) {
23+ while ( array [ atualLeft ] . preco < pivo . preco ) {
24+ atualLeft ++ ;
25+ }
26+ while ( array [ atualRight ] . preco > pivo . preco ) {
27+ atualRight -- ;
28+ }
29+ if ( atualLeft <= atualRight ) {
30+ trocaLugar ( array , atualLeft , atualRight ) ;
31+ atualLeft ++
32+ atualRight --
33+ }
34+ }
35+ return atualLeft ;
36+ }
37+ console . log ( quickSort ( listaLivros , 0 , listaLivros . length - 1 ) ) ;
You can’t perform that action at this time.
0 commit comments