60 564
Assignments Done
97,9%
Successfully Done
In April 2018

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

Question #58078
how to sort an array list without using a collection
Expert's answer
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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions