Reputation: 85
I know how to sort generic array type list using build-in methods like .sort() etc. But, I want to sort the list manually using for-loop. Can anyone help me on this method? Here is my ListClass
package AlgorithmAndDataStructures;
import java.util.Arrays;
public class ListClass<T extends Comparable<T>>{
private static final int MAX_SIZE_OF_LIST = 100;
/**
* This class is having definitions for:-
* Generic List Class Variables
* Constructor for Creating Class Objects
* Method: Adding a Element to the End of List
* Method: Adding a Element at anywhere/ particular place
* Method: Checking List is full or not.
* Method: Checking List is Empty or Not.
* Method: Displaying All Elements of List
* Method: Making New Space for new element addition.
* Method: Sorting a List
*
*/
// Declaring Array and Variables
private T[] listArray;
private int totalElements;
// Constructor For ListClass
@SuppressWarnings("unchecked")
public ListClass(int listSize) { // entered by the user on runtime
totalElements = 0;
listArray = (T[]) new Object[listSize];
}
// Method For Adding an Element
public boolean addElement(T newElement)
{
boolean isElementAdded = true;
if(!isListFull()) {
listArray[totalElements] = newElement;
totalElements++;
}
else
System.out.println("Sorry, the list is full so, new element can not be added.");
isElementAdded = false;
return isElementAdded;
}
// length = totalElements
// Method for Adding/Inserting Element in any Particular Place
public boolean addSpecific(int newLocation, T newElement) {
boolean elementAdded = true;
if (!isListFull() && (newLocation >= 1) && (newLocation <= totalElements +1) )
{
newSpace(newLocation);
listArray[newLocation -1] = newElement;
totalElements++;
}
else {
elementAdded = false;
}
return elementAdded;
}
// Method for Displaying The List Elements
public void displayListElement() {
if(isListEmpty())
{
System.out.println("Sorry, there is no element in the List!");
}
else
{
for(int elements = 0; elements < totalElements; elements++ ) {
System.out.println((listArray[elements]));
}
System.out.println("All elements has been displayed!");
}
}
// Method for Checking if List is Empty or Number of elements = 0
public boolean isListEmpty() {
return totalElements == 0;
}
// Method for Checking is List is full or not.
public boolean isListFull()
{
return totalElements == MAX_SIZE_OF_LIST;
}
private void newSpace( int newLocation)
{
// assert is a method predefined; indicator for index number
assert (newLocation >=1) && (newLocation <= totalElements +1);
int newIndicator = newLocation -1;
int lastIndicator = totalElements -1;
/**
* For Shifting Elements to Next Indexes
*/
for ( int sign = lastIndicator; sign >= newIndicator; sign--)
listArray[sign +1] = listArray[sign];
}
// Removing / Deleting All Elements of Generic List of Type Array
// Build in Method for sorting
public void sort() {
Arrays.sort(listArray, 0, totalElements);
}
}
In the end of class, you can see there is a build in method to sort. It is working fine. But, I want to use for-loop. Here is my driver program too.
package AlgorithmAndDataStructures;
public class DriverListClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
ListClass<Integer> listObjectInt = new ListClass<Integer>(10);
listObjectInt.addElement(12);
listObjectInt.addElement(17);
listObjectInt.addElement(90);
listObjectInt.addElement(53);
listObjectInt.addSpecific(3, 56);
listObjectInt.displayListElement();
listObjectInt.sort();
listObjectInt.displayListElement();
// String List
ListClass<String> listObjectStr = new ListClass<String>(4);
listObjectStr.addElement("Suman");
listObjectStr.addElement("Armaan");
listObjectStr.addElement("Atif");
listObjectStr.addElement("Tauseef");
listObjectStr.displayListElement();
}
}
UPDATED SORT METHOD IN ListClass
// Bubble Sort
public void bubbleSort(T[] list) {
int n = list.length - 1;
while (n != 0) {
int i;
for ( i = 0; i < n; i++) {
if (( list[i]).compareTo(list[i + 1]) > 0) {
T temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
}
}
n= i-1;
}
}
Now, I am confused in the driver program on how to call it?
listObjectInt.bubbleSort(WHAT_SHOULD_BE_HERE?);
Looking forward to a good help! Thanks!
Upvotes: 1
Views: 1209
Reputation: 21
You CANT compare two objects without any information about them. It must implement some interface. You can create your own interface for that?
Upvotes: 0
Reputation: 176
I think your teacher asked you to do is similar to Bubble Sort or Selection sort,and in your test ,if genericType is String ,the sort method is Incorrect.
if you require generic sort,you can Implement Comparable.(not for-loop)
this is correct if genericType is Interger:
public void sort() {
//Arrays.sort(listArray, 0, totalElements);
for(int i = 0 ;i<totalElements;i++){
for(int j=0;j<totalElements-i-1;j++){
System.out.println(i+"+"+j);
if((int)listArray[j]>(int)listArray[j+1]){
T temp = (T) listArray[j];
listArray[j] = (T) listArray[j+1];
listArray[j + 1] = temp;
}
}
}
}
My English is very poor,I am not sure if this is what exactly you want..
Upvotes: 1
Reputation: 27986
There are many sorting algorithms you can use to do this (see https://en.wikipedia.org/wiki/Sorting_algorithm for a list of some of the more common ones). One of the simplest is a bubble sort which uses a for
loop wrapped in a while
loop.
I'll give you some pseudo-code for it - it should be relatively straightforward to convert to code.
while not sorted
for each element in the list after the first one
if the element is larger than the previous one
swap the element with the previous one
Upvotes: 1
Reputation: 21
Make your type T extends Comparable and use the compareTo method of your objects
Upvotes: 0