Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners write a progam for bubble sort using file read nd write?
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Lunakar Reddy
write a progam for bubble sort using file read nd write?
1 Answer(s)      3 years and a month ago
Posted in : Java Beginners


hi, please give the code


Advertisement
View Answers

November 2, 2011 at 3:33 PM


import java.io.*;
import java.util.*;
public class FilebubbleSortExample{
  public static void main(String a[])throws Exception{
  ArrayList<Integer> list=new ArrayList<Integer>();
  BufferedReader br=new BufferedReader(new FileReader("num.txt"));
  BufferedWriter bw=new BufferedWriter(new FileWriter("new.txt",true));
  String st="";
  while((st=br.readLine())!=null){
      list.add(new Integer(Integer.parseInt(st)));
  }
  int i;
  int array[] = toIntArray(list);
  System.out.println("Values Before the sort:\n");
  for(i = 0; i < array.length; i++)
  System.out.print(array[i]+"  ");
  System.out.println();
  bubble_srt(array, array.length);
  System.out.print("Values after the sort:\n");
  for(i = 0; i <array.length; i++){
      System.out.print(array[i]+"  ");
      bw.write(Integer.toString(array[i]));
      bw.newLine();
  }
  bw.close();
  System.out.println();
  }
  public static void bubble_srt( int a[], int n ){
  int i, j,t=0;
  for(i = 0; i < n; i++){
          for(j = 1; j < (n-i); j++){
              if(a[j-1] > a[j]){
                  t = a[j-1];
                  a[j-1]=a[j];
                  a[j]=t;
              }
          }
      }
   }
   static int[] toIntArray(List<Integer> integerList) {
            int[] intArray = new int[integerList.size()];
            for (int i = 0; i < integerList.size(); i++) {
             intArray[i] = integerList.get(i);
            }
            return intArray;
       }
 }


Related Tutorials/Questions & Answers:
Advertisements
Advertisements
 

 

 

DMCA.com