Answer to Question #58078 in Java | JSP | JSF for steven johnson

Question #58078
how to sort an array list without using a collection
1
Expert's answer
2016-03-09T08:36:42-0500
package com.company;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Main {

public static void main(String[] args)throws Exception {
List<Integer> integerList = getIntegerList();
bubbleSort(integerList);

}
public static List<Integer> getIntegerList() throws IOException {
List<Integer> list = new ArrayList<Integer>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int N = 5;
for (int i=0;i<N;i++)
{
list.add(Integer.parseInt(reader.readLine()));
}
return list;
}
public static void bubbleSort(List<Integer> arr){
for(int i = arr.size()-1 ; i > 0 ; i--){
for(int j = 0 ; j < i ; j++){
if( arr.get(j) > arr.get(j+1)){
int tmp = arr.get(j);
arr.set(j,arr.get(j+1));
arr.set((j+1),tmp);
}
}
}
Iterator<Integer> iterator = arr.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS