Más contenido relacionado
Más de Daisuke Kasuya (20)
Scala関西ビギナーズ
- 8. L Q C A K B I J F R H D N T E O S G M P
14年2月8日土曜日
- 9. L Q C A K B I J F R H D N T E O S G M P
pivot
を決める
14年2月8日土曜日
- 10. L Q C A K B I J F R H D N T E O S G M P
C A K B I J F H D E G L Q R N T O S M P
pivotより小さい
グループと
大きいグループに分ける
14年2月8日土曜日
- 11. L Q C A K B I J F R H D N T E O S G M P
C A K B I J F H D E G L Q R N T O S M P
A B C K I
J F H D E G L
繰り返す
14年2月8日土曜日
R N M O P Q T S
- 13. public class QuickSort {
public static List<Integer> sort(List<Integer> list) {
if (list.isEmpty()) {
return list;
}
Integer pivot = list.get(0);
List<Integer> listWithoutPivot = list.subList(1, list.size());
List<Integer> leftSide = new LinkedList<>();
for (Integer elem : listWithoutPivot) {
if (elem <= pivot) {
leftSide.add(elem);
}
}
List<Integer> rightSide = new LinkedList<>();
for (Integer elem : listWithoutPivot) {
if (elem > pivot) {
rightSide.add(elem);
}
}
List<Integer> result = new ArrayList<>();
result.addAll(sort(leftSide));
result.add(pivot);
result.addAll(sort(rightSide));
return result;
}
}
参考: http://nikcode.blogspot.jp/2013/10/quick-sort-scala-vs-java.html
14年2月8日土曜日
- 14. public class QuickSort {
public static List<Integer> sort(List<Integer> list) {
if (list.isEmpty()) {
return list;
}
Integer pivot = list.get(0);
List<Integer> listWithoutPivot = list.subList(1, list.size());
List<Integer> leftSide = new LinkedList<>();
for (Integer elem : listWithoutPivot) {
if (elem <= pivot) {
leftSide.add(elem);
}
}
listの先頭をpivotとし、
それ以外のlistを取り出す
List<Integer> rightSide = new LinkedList<>();
for (Integer elem : listWithoutPivot) {
if (elem > pivot) {
rightSide.add(elem);
}
}
List<Integer> result = new ArrayList<>();
result.addAll(sort(leftSide));
result.add(pivot);
result.addAll(sort(rightSide));
return result;
}
}
参考: http://nikcode.blogspot.jp/2013/10/quick-sort-scala-vs-java.html
14年2月8日土曜日
- 15. public class QuickSort {
public static List<Integer> sort(List<Integer> list) {
if (list.isEmpty()) {
return list;
}
Integer pivot = list.get(0);
List<Integer> listWithoutPivot = list.subList(1, list.size());
List<Integer> leftSide = new LinkedList<>();
for (Integer elem : listWithoutPivot) {
if (elem <= pivot) {
leftSide.add(elem);
}
}
pivot以下のグループ
List<Integer> rightSide = new LinkedList<>();
for (Integer elem : listWithoutPivot) {
if (elem > pivot) {
rightSide.add(elem);
}
}
List<Integer> result = new ArrayList<>();
result.addAll(sort(leftSide));
result.add(pivot);
result.addAll(sort(rightSide));
return result;
}
}
参考: http://nikcode.blogspot.jp/2013/10/quick-sort-scala-vs-java.html
14年2月8日土曜日
- 16. public class QuickSort {
public static List<Integer> sort(List<Integer> list) {
if (list.isEmpty()) {
return list;
}
Integer pivot = list.get(0);
List<Integer> listWithoutPivot = list.subList(1, list.size());
List<Integer> leftSide = new LinkedList<>();
for (Integer elem : listWithoutPivot) {
if (elem <= pivot) {
leftSide.add(elem);
}
}
List<Integer> rightSide = new LinkedList<>();
for (Integer elem : listWithoutPivot) {
if (elem > pivot) {
rightSide.add(elem);
}
}
pivotより大きいグループ
List<Integer> result = new ArrayList<>();
result.addAll(sort(leftSide));
result.add(pivot);
result.addAll(sort(rightSide));
return result;
}
}
参考: http://nikcode.blogspot.jp/2013/10/quick-sort-scala-vs-java.html
14年2月8日土曜日
- 17. public class QuickSort {
public static List<Integer> sort(List<Integer> list) {
if (list.isEmpty()) {
return list;
}
Integer pivot = list.get(0);
List<Integer> listWithoutPivot = list.subList(1, list.size());
List<Integer> leftSide = new LinkedList<>();
for (Integer elem : listWithoutPivot) {
if (elem <= pivot) {
leftSide.add(elem);
}
}
List<Integer> rightSide = new LinkedList<>();
for (Integer elem : listWithoutPivot) {
if (elem > pivot) {
rightSide.add(elem);
}
}
List<Integer> result = new ArrayList<>();
result.addAll(sort(leftSide));
result.add(pivot);
result.addAll(sort(rightSide));
pivot以下のグループ、
pivot、
pivotより大きいグループ
それぞれを再帰的に連結
return result;
}
}
参考: http://nikcode.blogspot.jp/2013/10/quick-sort-scala-vs-java.html
14年2月8日土曜日
- 18. public class QuickSort {
public static List<Integer> sort(List<Integer> list) {
if (list.isEmpty()) {
return list;
}
Integer pivot = list.get(0);
List<Integer> listWithoutPivot = list.subList(1, list.size());
List<Integer> leftSide = new LinkedList<>();
for (Integer elem : listWithoutPivot) {
if (elem <= pivot) {
leftSide.add(elem);
}
}
List<Integer> rightSide = new LinkedList<>();
for (Integer elem : listWithoutPivot) {
if (elem > pivot) {
rightSide.add(elem);
}
}
List<Integer> result = new ArrayList<>();
result.addAll(sort(leftSide));
result.add(pivot);
result.addAll(sort(rightSide));
return result;
}
}
参考: http://nikcode.blogspot.jp/2013/10/quick-sort-scala-vs-java.html
14年2月8日土曜日
- 19. public class QuickSort {
public static List<Integer> sort(List<Integer> list) {
if (list.isEmpty()) {
return list;
}
Integer pivot = list.get(0);
List<Integer> listWithoutPivot = list.subList(1, list.size());
List<Integer> leftSide = new LinkedList<>();
for (Integer elem : listWithoutPivot) {
if (elem <= pivot) {
leftSide.add(elem);
}
}
ちゃんと読めば意図を理解できるけど、
List<Integer> rightSide = new LinkedList<>();
ぱっと見ではわかりづらくないですか?
for (Integer elem : listWithoutPivot) {
if (elem > pivot) {
rightSide.add(elem);
}
}
List<Integer> result = new ArrayList<>();
result.addAll(sort(leftSide));
result.add(pivot);
result.addAll(sort(rightSide));
return result;
}
}
参考: http://nikcode.blogspot.jp/2013/10/quick-sort-scala-vs-java.html
14年2月8日土曜日
- 21. def qSort[T <% Ordered[T]](data: List[T]): List[T] = {
data match {
case Nil => data
case x::xs => {
qSort(xs.filter(_ <= x)) ++ List(x) ++ qSort(xs.filter(x < _))
}
}
}
14年2月8日土曜日
- 22. def qSort[T <% Ordered[T]](data: List[T]): List[T] = {
data match {
listの先頭をpivotとし、
case Nil => data
それ以外のlistを取り出す
case x::xs => {
qSort(xs.filter(_ <= x)) ++ List(x) ++ qSort(xs.filter(x < _))
}
}
}
14年2月8日土曜日
- 23. def qSort[T <% Ordered[T]](data: List[T]): List[T] = {
data match {
case Nil => data
case x::xs => {
qSort(xs.filter(_ <= x)) ++ List(x) ++ qSort(xs.filter(x < _))
}
}
pivot以下のグループ
}
14年2月8日土曜日
- 24. def qSort[T <% Ordered[T]](data: List[T]): List[T] = {
data match {
case Nil => data
case x::xs => {
qSort(xs.filter(_ <= x)) ++ List(x) ++ qSort(xs.filter(x < _))
}
}
}
pivotより大きいグループ
14年2月8日土曜日
- 25. def qSort[T <% Ordered[T]](data: List[T]): List[T] = {
data match {
case Nil => data
case x::xs => {
qSort(xs.filter(_ <= x)) ++ List(x) ++ qSort(xs.filter(x < _))
}
}
pivot以下のグループ、
}
pivot、
pivotより大きいグループ
それぞれを再帰的に連結
14年2月8日土曜日
- 26. def qSort[T <% Ordered[T]](data: List[T]): List[T] = {
data match {
case Nil => data
case x::xs => {
qSort(xs.filter(_ <= x)) ++ List(x) ++ qSort(xs.filter(x < _))
}
}
}
14年2月8日土曜日
- 27. def qSort[T <% Ordered[T]](data: List[T]): List[T] = {
data match {
case Nil => data
case x::xs => {
qSort(xs.filter(_ <= x)) ++ List(x) ++ qSort(xs.filter(x < _))
}
}
}
直感的な気がする!!!
14年2月8日土曜日
- 28. CAUTION!!
def qSort[T <% Ordered[T]](data: List[T]): List[T] = {
data match {
case Nil => data
case x::xs => {
qSort(xs.filter(_ <= x)) ++ List(x) ++ qSort(xs.filter(x < _))
}
}
}
実はこれ、末尾再帰じゃないので
わりとすぐにスタックオーバーフロー!!
14年2月8日土曜日