Sorting

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2007
Posts: 50
Reputation: volscolts16 is an unknown quantity at this point 
Solved Threads: 0
volscolts16's Avatar
volscolts16 volscolts16 is offline Offline
Junior Poster in Training

Sorting

 
0
  #1
Nov 12th, 2007
Hi All,
I have a new assignment which I have worked most of the way out, except I have one issue. When I sort the large text file it does not print out all the information at that moment, I have to hit enter a couple of times before all the infroamtion comes out, not like it does for the small text file? Thanks in Advance!!

Situation:
Large file?
yes
Merge sort done!
MergeSort calls: 59999
Merge Calls: 29999
Time Elapsed: 701 Milliseconds



Selection sort took 449985000 iterations.
Time Elapsed: 8892 Milliseconds

versus

Large file?
no
Merge sort done!
MergeSort calls: 1999
Merge Calls: 999
Time Elapsed: 851 Milliseconds
Selection sort took 499500 iterations.
Time Elapsed: 20 Milliseconds

Here is the assignment:

For this lab, implement selection sort and merge sort. Add counters to the methods to track the number of comparisons and assignments that occur in each algorithm.

Implement a metric that allows you to calculate the running time of the algorithms. HINT: java.util provides time, timer and timestamp classes.

Sort the Small.txt and Large.txt files and write a brief comparison of the algorithms you implemented.

Here is the code:
  1. import java.util.ArrayList;
  2.  
  3. public class Sort
  4. {
  5. int i;
  6. int j;
  7. //make arraylist
  8. ArrayList<Integer> a = new ArrayList();
  9. // count how many iterations are performed during the sort
  10. private long iterations = 0;
  11.  
  12. public ArrayList<Integer> selectionSort(ArrayList<Integer> a)
  13. {
  14. //checks every other number and gives one the name i and 1 j
  15. for (int i=0; i < a.size()-1; i++)
  16. {
  17.  
  18. for (int j=i+1; j < a.size(); j++)
  19. {
  20. //check to see which is greater to put them in order
  21. if (a.get(i) > a.get(j))
  22. {
  23. //If x is the greater element, exchange elements
  24. int temp = a.get(i);
  25. a.add(i,a.get(j));
  26. a.add(j,temp);
  27. //iterations++;
  28. }
  29. iterations++;
  30. }
  31. }
  32. System.out.println("Selection sort took " + iterations + " iterations.");
  33. //return list
  34. return a;
  35. }
  36. }
  37.  
  38. import java.util.ArrayList;
  39. public class Sorter
  40. {
  41. //declare as integers
  42. private int mergesort = 0;
  43. private int merge = 0;
  44. public ArrayList Sort(ArrayList<Integer> a)
  45. {
  46. //just make sure they're all zero\
  47. //uses method from bottom
  48. while(!leastToGreatest(a))
  49. {
  50. for(int x = 0; x < a.size() - 1; x++)
  51. {
  52. if (a.get(x) > a.get(x+1))
  53. //use change method below and sort
  54. a = change(a, x);
  55. }
  56. }
  57. return a;
  58. }
  59. // make a temporary instance and make instances of positions
  60. private ArrayList change(ArrayList<Integer> a, int position)
  61. {
  62. int temp = a.get(position);
  63. a.set(position, a.get(position+1));
  64. a.set(position+1, temp);
  65. return a;
  66. }
  67. private boolean leastToGreatest(ArrayList<Integer> a)
  68. {
  69. for(int x = 1; x < a.size(); x++)
  70. {
  71. //put ArrayList in order using this loop
  72. if(a.get(x) < a.get(x-1))
  73. return false;
  74. }
  75. return true;
  76. }
  77. // make array to hold positions
  78. public int[] getData()
  79. {
  80. return new int[] {mergesort, merge};
  81. }
  82.  
  83.  
  84.  
  85.  
  86. //LAB
  87. public ArrayList<Integer> MergeSort(ArrayList<Integer> a)
  88. {
  89. mergesort++;
  90. // make 3 lists to make merging easier
  91. ArrayList<Integer> result = new ArrayList<Integer>();
  92. ArrayList<Integer> left = new ArrayList<Integer>();
  93. ArrayList<Integer> right = new ArrayList<Integer>();
  94. //already sorted in this case
  95. if(a.size() <= 1)
  96. return a;
  97. else
  98. {
  99. //perform these loops if arraylist is unsorted
  100. int half = a.size() / 2;
  101. for (int x = 0; x < half; x++)
  102. {
  103. left.add(a.get(x));
  104. }
  105. for (int x = half; x < a.size(); x++)
  106. {
  107. right.add(a.get(x));
  108. }
  109. left = MergeSort(left);
  110. right = MergeSort(right);
  111. result = Merge(left, right);
  112. return result;
  113. }
  114. }
  115.  
  116. private ArrayList<Integer> Merge(ArrayList<Integer> left, ArrayList<Integer> right)
  117. {
  118. //merge method used to complete sorting
  119. merge++;
  120. ArrayList<Integer> result = new ArrayList<Integer>();
  121. //has to be greater than 0 otherwise no merging is needed
  122. while(left.size() > 0 && right.size() > 0)
  123. {
  124. //put the lower numbers on the left
  125. if (left.get(0) <= right.get(0))
  126. {
  127. result.add(left.get(0));
  128. left.remove(0);
  129. }
  130. else
  131. {
  132. result.add(right.get(0));
  133. right.remove(0);
  134. }
  135. }
  136. //put the higher numbers on the right
  137. if ( left.size() > right.size())
  138. result.addAll(left);
  139. else if (left.size() <= right.size())
  140. result.addAll(right);
  141. return result;
  142. }
  143. }
  144.  
  145.  
  146. import java.util.*;
  147. import java.io.*;
  148. public class test
  149. {
  150. // main
  151. public static void main(String[] args)
  152. {
  153. // make longs for timestamps
  154. long begin, end;
  155. //import scanner for user input
  156. Scanner userinput = new Scanner(System.in);
  157. System.out.println("Large file?");
  158. String input = userinput.nextLine();
  159. String first = "";
  160. String outputFile = "";
  161. String outputFile2 = "";
  162. //create if statement to determine whether user wants a small text file or a large one
  163. if(input.equalsIgnoreCase("yes"))
  164. {
  165. // read in file
  166. first = "H:\\Computer Science 160\\Lab 9\\Large.txt";
  167. outputFile = "H:\\largeSorted.txt";
  168. outputFile2 = "H:\\largeSelectionSort.txt";
  169. }
  170. else if (input.equalsIgnoreCase("no"))
  171. {
  172. //read in file
  173. first = "H:\\Computer Science 160\\Lab 9\\small.txt";
  174. outputFile = "H:\\smallSorted.txt";
  175. outputFile2 = "H:\\smallSelectionSort.txt";
  176. }
  177. try
  178. {
  179. //apply sorting method
  180. Scanner scn = new Scanner(new File(first));
  181. Sorter s = new Sorter();
  182. Sort sort = new Sort();
  183. //use arraylist
  184. ArrayList<Integer> p = new ArrayList();
  185. while (scn.hasNext())
  186. {
  187. p.add(scn.nextInt());
  188. }
  189.  
  190. //INSERT TIME CALCULATION STUFF
  191. begin = System.currentTimeMillis();
  192. p = s.MergeSort(p);
  193. end = System.currentTimeMillis();
  194. //print back the output placed in the new file
  195. PrintWriter output = new PrintWriter(new FileWriter(outputFile));
  196. // get the entire size
  197. for(int x = 0; x < p.size(); x++)
  198. output.println(p.get(x));
  199. System.out.println("Merge sort done!");
  200. System.out.println("MergeSort calls: " + s.getData()[0] + "\nMerge Calls: " + s.getData()[1]);
  201. System.out.println("Time Elapsed: " + (end - begin) + " Milliseconds");
  202. output.close();
  203. begin = System.currentTimeMillis();
  204. p = sort.selectionSort(p);
  205. end = System.currentTimeMillis();
  206. //print out the second sort for merge
  207. PrintWriter output2 = new PrintWriter(new FileWriter(outputFile2));
  208. for(int x = 0; x < p.size(); x++)
  209. output2.println(p.get(x));
  210. System.out.println("Time Elapsed: " + (end - begin) + " Milliseconds");
  211. output2.close();
  212. }
  213. //catch a general exception for ease
  214. catch (Exception e)
  215. {
  216. System.out.println(e);
  217. }
  218. }
  219. }
Last edited by ~s.o.s~; Nov 12th, 2007 at 12:19 pm. Reason: Added code tags, learn to use them.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 56
Reputation: vigneshvh is an unknown quantity at this point 
Solved Threads: 5
vigneshvh vigneshvh is offline Offline
Junior Poster in Training

Re: Sorting

 
0
  #2
Nov 15th, 2007
#include <stdio.h>
#define MAX 10
void swap(int *x,int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void bsort(int list[], int n)
{
int i,j;
for(i=0;i<(n-1);i++)
for(j=0;j<(n-(i+1));j++)
if(list[j] > list[j+1])
swap(&list[j],&list[j+1]);
}


void readlist(int list[],int n)
{
int i;
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&list[i]);
}

void printlist(int list[],int n)
{
int i;
printf("The elements of the list are: \n");
for(i=0;i<n;i++)
printf("%d\t",list[i]);
}

void main()
{
int list[MAX], n;
printf("Enter the number of elements in the list max = 10\n");
scanf("%d",&n);
readlist(list,n);
printf("The list before sorting is:\n");
printlist(list,n);
bsort(list,n);
printf("The list after sorting is:\n");
printlist(list,n);
}

try it
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 517
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Industrious Poster

Re: Sorting

 
0
  #3
Nov 15th, 2007
Originally Posted by vigneshvh View Post
... try it
You mean try converting it to Java, since that is the language he is using?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 34
Reputation: mohanrobin has a little shameless behaviour in the past 
Solved Threads: 0
mohanrobin mohanrobin is offline Offline
Light Poster

Re: Sorting

 
-1
  #4
Nov 16th, 2007
i am mohan robin.i know java a little,but can understand the language.i think he want the program to convert in java language ,which is already in c++ language .try,thanks.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC