generated from nighthawkcoders/spring_portfolio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bubble sort fix, desc, added sorting steps
- Loading branch information
1 parent
75b7dbf
commit ee1e0ea
Showing
8 changed files
with
94 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 23 additions & 8 deletions
31
src/main/java/com/nighthawk/spring_portfolio/mvc/sorting/Bubble.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,39 @@ | ||
package com.nighthawk.spring_portfolio.mvc.sorting; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import com.nighthawk.spring_portfolio.mvc.art.Art; | ||
|
||
public class Bubble extends Sorting { | ||
@Override | ||
public List<Art> sortArt(List<Art> unsortedArts) { | ||
public SortingResult sortArt(List<Art> unsortedArts) { | ||
SortingResult result = new SortingResult(); | ||
int n = unsortedArts.size(); | ||
Art temp; | ||
for (int i=0; i<n-1; i++){ | ||
for (int j=0; j<n-i-1; j++){ | ||
if (unsortedArts.get(j+1).getLike() > unsortedArts.get(j).getLike()){ | ||
//swap them if first one is less, should be GREATEST to LEAST order | ||
for(int i = 0; i < n - 1; i++) { | ||
boolean swapped = false; | ||
|
||
for (int j = 0; j < n - 1 - 1; j++) { | ||
if(unsortedArts.get(j).getLike() < unsortedArts.get(j + 1).getLike()) { | ||
temp = unsortedArts.get(j); | ||
unsortedArts.set(j, unsortedArts.get(j)); | ||
unsortedArts.set(j+1, temp); | ||
unsortedArts.set(j, unsortedArts.get(j + 1)); | ||
unsortedArts.set(j + 1, temp); | ||
|
||
swapped = true; | ||
|
||
List<Integer> step = unsortedArts.stream(). | ||
map(art -> art.getLike()). | ||
collect(Collectors.toList()); | ||
|
||
result.sortingSteps.add(step); | ||
} | ||
} | ||
|
||
if (!swapped) break; | ||
} | ||
return unsortedArts; | ||
|
||
result.sortedArts = unsortedArts; | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 10 additions & 9 deletions
19
src/main/java/com/nighthawk/spring_portfolio/mvc/sorting/Sorting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,27 @@ | ||
package com.nighthawk.spring_portfolio.mvc.sorting; | ||
|
||
import java.util.ArrayList; | ||
//import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.nighthawk.spring_portfolio.mvc.art.Art; | ||
|
||
public class Sorting { | ||
|
||
//override to create specific sort | ||
protected List<Art> sortArt(List<Art> unsortedArts) { | ||
return new ArrayList<>(); | ||
protected SortingResult sortArt(List<Art> unsortedArts) { | ||
return new SortingResult(); | ||
} | ||
|
||
public SortingResult getSortingResult(List<Art> unsortedArts) { | ||
long startTime = System.nanoTime(); | ||
List<Art> result = sortArt(unsortedArts); | ||
SortingResult result = sortArt(unsortedArts); | ||
long endTime = System.nanoTime(); | ||
long finalTime = endTime - startTime; | ||
SortingResult sResult = new SortingResult(); | ||
sResult.sortTime = finalTime; | ||
sResult.sortedArts = result; | ||
return sResult; | ||
result.sortTime = finalTime; | ||
//result.steps.add(unsortedArts); | ||
return result; | ||
} | ||
|
||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.