I'm a novice and started diving into data structures and algorithms recently. I was taught bubble sort recently and I found that the original bubble sort algorithm is so inefficient, so I wrote a modified, yet simple version of it.
Algorithm:
- Loop through the provided array and store the index of the max element
- Swap the last element with the max element
- Decrease the total looping length by 1 and repeat
Code:
import java.util.Scanner;
class Methods extends Main
{
int[] getArray(int len)
{
int[] array = new int[len];
for (int i = 0; i < array.length; i++)
{
array[i] = sc.nextInt();
}
return array;
}
int [] pushMaxSort(int[] arr)
{
int len = arr.length;
while (len>0)
{
int max=arr[0];
int index=0;
for(int i=0; i<len;i++)
{
if(arr[i]>max)
{
max = arr[i];
index = i;
}
}
int temp = arr[len-1];
arr[len-1] = arr[index];
arr[index] = temp;
len--;
}
return arr;
}
void printArr(int []arr)
{
System.out.println("sorted array :");
for(int i=0;i<arr.length;i++)
{
System.out.print("\t" +arr[i]);
}
}
}
class Main
{
static Scanner sc = new Scanner(System.in);
public static void main(String[] args)
{
long t = System.currentTimeMillis();
Methods o = new Methods();
o.printArr(o.pushMaxSort(o.getArray(sc.nextInt())));
long t2 = System.currentTimeMillis();
System.out.println("\n\n time " + (t2-t));
}
}
Also, I need to learn to describe complexity (using Big O notation). I am vaguely guessing this has \$O(n \log{n})\$ but please let me know of the exact running time.
-
1\$\begingroup\$ Once you have code it is typically simple to see what the complexity is. You have for loop in while loop, which in worst case run with whole length => O(n^2). Sure, it's 0.5 * n^2 but we still say it's O(n^2)... \$\endgroup\$Betlista– Betlista2018年02月01日 13:47:27 +00:00Commented Feb 1, 2018 at 13:47
-
\$\begingroup\$ Just to be clear, by "DS", do you mean "data structures"? If so, then write "data structures". Don't assume that everyone knows what "DS" means. \$\endgroup\$Simon Forsberg– Simon Forsberg2018年02月01日 15:46:57 +00:00Commented Feb 1, 2018 at 15:46
-
\$\begingroup\$ Please compare your algorithm to selection sort. \$\endgroup\$greybeard– greybeard2018年02月01日 16:56:12 +00:00Commented Feb 1, 2018 at 16:56
1 Answer 1
The design makes no sense in terms of Object-Oriented principles:
The inheritance relation between the two classes makes no logical sense. It seems to serve the sole purpose of sharing the
scanner
instance. Inheritance is more than mechanism for sharing variables. It has application (aka business) meaning. It defines "is kind of" relationship: aDog
is a kind ofAnimal
.The class names say absolutely nothing about what the code does. Well, one could argue that
Main
serves as the program's entry point. I've seen this "naming convention" in actual java projects so that's passable. I would call it something likeArraySorter
. It is also acceptable to add themain()
method to the other methods instead of putting in a separate class.Methods
class is another matter: There has to be better name that describes what the collection of methods do. In conclusion, it is up to you whetherPushMaxSorter
has its ownmain()
method or keep it separate.So, if we look at
Methods
asPushMaxSorter
that is a class that implements your improved bubble sort algorithm, thengetArray()
can be regarded as a constructor. and it should take aScanner
instance as argument.So, from the above constructor, we deduce that
PushMaxSorter
holds the int array as internal data structure. So the sort method should accept no arg and return void and if you wish you can addgetArray()
that is a "classic" getter method: allows access to the instance variable.regarding
printArr()
: so we now can deduce that this method also accept no arg. It is considered "best parctice" that a class with a data structure (or just a bunch of variables) will have a string representation of its internal state by overridingObject
'stoString()
method. This allows the caller to conrtol to which output theString
is written.
Regarding complexity, I admit this is not my expertise. However, I do not see any signs of exponential iteration on the data structure. Your algorithm may improve upon the performance of classic bubble sort, but complexity is about scaling: comparing the performance of the same algorithm on input that is increasing in size. I'd say complexity here is the same as classic bubble sort, which is O(n^2)
Note: a more robust and extensible design will say that you need to decouple the data structure from the sorting algorithm (much like the Java Collections framework) so that you can apply your improved algorithm not only to array of integers. But I think that is an "advanced" topic that requires deeper study of advanced OO principles.
-
\$\begingroup\$ Thanks a ton Sharon. I surely need to learn design principles and you taught me some. \$\endgroup\$SandyG– SandyG2018年02月01日 13:53:06 +00:00Commented Feb 1, 2018 at 13:53