Skip to content

Commit

Permalink
bubble sort fix, desc, added sorting steps
Browse files Browse the repository at this point in the history
  • Loading branch information
vivianknee committed Dec 8, 2023
1 parent 75b7dbf commit ee1e0ea
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,26 @@ public ResponseEntity<Art> setLike(@PathVariable long id) {
}

@PostMapping("/sorted/{method}")
public ResponseEntity<SortingResult> getSortedArts(String method) {
public ResponseEntity<SortingResult> getSortedArts(@PathVariable String method) throws Exception {
// ResponseEntity returns List of Jokes provide by JPA findAll()
List<Art> arts = repository.findAll();

Sorting sorter = new Insertion();
if (method == "bubble") {
Sorting sorter = new Bubble();
if (method.contains("bubble")) {
sorter = new Bubble();
}
if (method == "insertion") {
else if (method.contains("insertion")) {
sorter = new Insertion();
}
if (method == "merge") {
else if (method.contains("merge")) {
sorter = new Merge();
}
if (method == "selection") {
else if (method.contains("selection")) {
sorter = new Selection();
}
else {
throw new Exception(method);
}

// use your sorting class here to sort the list
SortingResult sortResult = sorter.getSortingResult(arts);
Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

//import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.nighthawk.spring_portfolio.mvc.art.Art;

public class Insertion extends Sorting {
@Override
public List<Art> sortArt(List<Art> unsortedArts) {
public SortingResult sortArt(List<Art> unsortedArts) {
SortingResult result = new SortingResult();

for (int i = 1; i < unsortedArts.size(); i++) {
Art currArt = unsortedArts.get(i);
int j = i - 1;
Expand All @@ -16,10 +19,22 @@ public List<Art> sortArt(List<Art> unsortedArts) {
while (j >= 0 && unsortedArts.get(j).getLike() < currArt.getLike()) {
unsortedArts.set(j + 1, unsortedArts.get(j));
j -= 1;
List<Integer> step = unsortedArts.stream().
map(art -> art.getLike()).
collect(Collectors.toList());

result.sortingSteps.add(step);
}
// put currArt at correct spot
unsortedArts.set(j + 1, currArt);

List<Integer> step = unsortedArts.stream().
map(art -> art.getLike()).
collect(Collectors.toList());

result.sortingSteps.add(step);
}
return unsortedArts;
result.sortedArts = unsortedArts;
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package com.nighthawk.spring_portfolio.mvc.sorting;

import java.util.ArrayList;
//import java.util.Comparator;
import java.util.List;

import com.nighthawk.spring_portfolio.mvc.art.Art;

public class Merge extends Sorting {
@Override
public List<Art> sortArt(List<Art> unsortedArts) {
public SortingResult sortArt(List<Art> unsortedArts) {
SortingResult result = new SortingResult();
List<Art> sorted = sortArtInternal(unsortedArts);
result.sortedArts = sorted;
return result;
}

private List<Art> sortArtInternal(List<Art> unsortedArts) {
// list empty or 1 element prob not gonna happen but precaution
if (unsortedArts.size()<=1){
return unsortedArts;
Expand All @@ -19,8 +25,8 @@ public List<Art> sortArt(List<Art> unsortedArts) {
List<Art> right = new ArrayList<>(unsortedArts.subList(midIndex, unsortedArts.size()));

//recursive to keep on splitting and sorting list
left = sortArt(left);
right = sortArt(right);
left = sortArtInternal(left);
right = sortArtInternal(right);

// merge sorted halves when done each time
return merge(left, right);
Expand All @@ -41,17 +47,19 @@ private List<Art> merge(List<Art> left, List<Art> right) {
}
}

// add leftoever elements from left
// add leftover elements from left
while (leftIndex < left.size()) {
mergeDone.add(left.get(leftIndex));
leftIndex++;
}

// add leftoever elements from right
// add leftover elements from right
while (rightIndex < right.size()) {
mergeDone.add(right.get(rightIndex));
rightIndex++;
}

return mergeDone;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,38 @@

//import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.nighthawk.spring_portfolio.mvc.art.Art;

public class Selection extends Sorting {
@Override
public List<Art> sortArt(List<Art> unsortedArts) {
public SortingResult sortArt(List<Art> unsortedArts) {
SortingResult result = new SortingResult();

int n = unsortedArts.size();

for (int i=0; i<n-1; i++){
// find min, will swap this with first element
int minIndex = i;
int maxIndex = i;
for (int j=i+1; j<n; j++){
if (unsortedArts.get(j).getLike() < unsortedArts.get(minIndex).getLike()){
minIndex = j;
if (unsortedArts.get(j).getLike() > unsortedArts.get(maxIndex).getLike()){
maxIndex = j;
}
}
//swap found min element with first element
Art temp = unsortedArts.get(minIndex);
unsortedArts.set(minIndex, unsortedArts.get(i));
Art temp = unsortedArts.get(maxIndex);
unsortedArts.set(maxIndex, unsortedArts.get(i));
unsortedArts.set(i, temp);

List<Integer> step = unsortedArts.stream().
map(art -> art.getLike()).
collect(Collectors.toList());

result.sortingSteps.add(step);
}
return unsortedArts;
result.sortedArts = unsortedArts;

return result;
}
}
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;
}



}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.nighthawk.spring_portfolio.mvc.sorting;

import java.util.ArrayList;
import java.util.List;

import com.nighthawk.spring_portfolio.mvc.art.Art;
Expand All @@ -8,4 +9,6 @@ public class SortingResult {
public List<Art> sortedArts;

public Long sortTime;

public List<List<Integer>> sortingSteps = new ArrayList<List<Integer>>();
}
Binary file modified volumes/sqlite.db
Binary file not shown.

0 comments on commit ee1e0ea

Please sign in to comment.