Jump to content

Java Sorting

Zygizz

Hi,

 

Am a student and we have the assignment in java. we need to create one additional sorting method besides sortbuble and sortjava methods we have in examples so:D basically am sitting all day and trying to figure out how I can write it and just kept getting an error or just methods not working and getting desperate :( ... I have first one example sorting from teacher and the second one is my method what am trying to write 

 

public void sortBuble() {
		if (first == null) {
			return;
		}
		for (;;) {
			boolean jauGerai = true;
			Node<E> e1 = first;
			for (Node<E> e2 = first.next; e2 != null; e2 = e2.next) {
				if (e1.element.compareTo(e2.element) > 0) {
					E t = e1.element;
					e1.element = e2.element;
					e2.element = t;
					jauGerai = false;
				}
				e1 = e2;
			}
			if (jauGerai) {
				return;
			}
		}
	}

	
	public void sortBuble(Comparator<E> c) {
		if (first == null) {
			return;
		}
		for (;;) {
			boolean jauGerai = true;
			Node<E> e1 = first;
			for (Node<E> e2 = first.next; e2 != null; e2 = e2.next) {
				if (c.compare(e1.element, e2.element) > 0) {
					E t = e1.element;
					e1.element = e2.element;
					e2.element = t;
					jauGerai = false;
				}
				e1 = e2;
			}
			if (jauGerai) {
				return;
			}
		}
	}
              
   
              
	public void DoSelectionSort(){

		for (Node<E> e1 = first ; e1 != null ; e1 = e1.next){
				for(Node<E> e2 = first.next; e2 != null ; e2 = e2.next){
					if (e1.element.compareTo(e2.element) > 0){
						E smallerNumber = e1.element;
						e1.element = e2.element;
						e2.element = smallerNumber;
					}
				}
		}
	}


	public void DoSelectionSort(Comparable<E> c){

		for (Node<E> e1 = first ; e1 != null ; e1 = e1.next){
			for(Node<E> e2 = first.next; e2 != null ; e2 = e2.next){
				if (e1.element.compareTo(e2.element) > 0){
					E smallerNumber = e1.element;
					e1.element = e2.element;
					e2.element = smallerNumber;
				}
			}
		}
	}

 

Link to comment
Share on other sites

Link to post
Share on other sites

It would really help to have a bit more context what is static variable first? Is it the List/element you want to sort ? BTW that example from your teacher is horrible bad practice of writing code. Its hardly readable. Assuming first is the list to be sorted why is the name first not for example numbers or numberList or i dont know wordList depending on what it contains. Should it be a generic approach ? He is using an endless for loop when he should use a do while loop, jauGerai means what? 

		if (first == null) {
			return;
		}
would lead to a failure if the list is initialized but empty later on in the method
when trying to access non existing elements also returning an empty instruction to exit his for loop im shaking my head.
In your second example you hand over a Comparable c that does nothing so its the same as your'
first method. And from a first look at your method i cant figure out your problem what error you 
get? Would maybe help. Also im not familiar with Node what does it do ?  
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×