Cramster.com - Homework Solutions, Lecture Notes, Exams, and Free Online Homework Help
Sign Up Now! Login Customer Support
McAfee Secure sites help keep you safe from identity theft, credit card fraud, spyware, spam, viruses and online scams
Problem Solved.
    Home    
    Homework Help    
   Answer Board   
    Resources (Beta)    
   
Member's Topic Headline:

JAVA - Sorting in reverse order **will rate lifesaver**

Know the answer? Have a better solution? Share it.
Sign Up Now for FREE!
Join the thousands of students
getting ahead in their classes.
Member Testimonials

Question:

Advertisement:

Answer | Ask New Question | Customize Profile | Leaderboards | 
FAQ

Member's Avatar

Scholar
Karma Points: 340
Respect (95%):
Date Posted: 7/23/2008 10:30:07 PM  Status: Closed
JAVA - Sorting in reverse order **will rate lifesaver**
Course Textbook Chapter Problem
N/A N/A N/A N/A
Question Details:
When the input has been sorted in reverse order, what is the running time of
a. Insertion sort
b. Shellsort
c. Mergesort
d. Quicksort

Bonus Point Alert! Earn +4 additional karma points for helping this annual member.

Answers:

Member's Avatar

Guru
Karma Points: 2,214
Date Posted: 7/23/2008 10:57:43 PM  Status: Live
Asker's Rating: Lifesaver   
Response:
Having a reverse order will result in worst case for run time
a. Insertion sort
- O(n2)
b. Shellsort - O(n2)
c. Mergesort - O(n log n)
d. Quicksort - O(n2)

More in-depth details from Wikipedia:
http://en.wikipedia.org/wiki/Insertion_sort
http://en.wikipedia.org/wiki/Shell_sort
http://en.wikipedia.org/wiki/Mergesort
http://en.wikipedia.org/wiki/Quicksort


cout << "Ex nihilio nihil fit" << endl; //C++
System.out.println("Tempus fugit"); //Java
msg: .ASCII "Noli me vocare, ego te vocabo\x00"   //ASM
main:    STRO   msg,d
            .END
            ZZ

Member's Avatar

Guru
Karma Points: 2,014
Date Posted: 7/24/2008 1:04:45 AM  Status: Live
Asker's Rating: Helpful   
Response:
Insertion Sort

Insertion sort
is a simple sorting algorithm, a comparison sort in which the sorted array (or list) is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort, but it has various advantages:
  • Simple to implement
  • Efficient on (quite) small data sets
  • Efficient on data sets which are already substantially sorted: it runs in O(n + d) time, where d is the number of inversions
  • More efficient in practice than most other simple O(n2) algorithms such as selection sort or bubble sort: the average time is n2/4 and it is linear in the best case
  • Stable (does not change the relative order of elements with equal keys)
  • In-place (only requires a constant amount O(1) of extra memory space)
  • It is an online algorithm, in that it can sort a list as it receives it.
Example:
insertionSort(array A)
for i = 1 to length[A]-1 do
begin
value = A[i]
j = i-1
while j >= 0 and A[j] > value do
begin
A[j + 1] = A[j]
j = j-1
end
A[j+1] = value
end

Shell Sort

Shell sort is a sorting algorithm that is a generalization of insertion sort, with two observations:

  • insertion sort is efficient if the input is "almost sorted", and
  • insertion sort is typically inefficient because it moves values just one position at a time.
Example:
void shell_sort(int A[], int size) {
int i, j, increment, temp;
increment = size / 2;
while (increment > 0) {
for (i = increment; i < size; i++) {
j = i;
temp = A[i];
while ((j >= increment) && (A[j-increment] > temp)) {
A[j] = A[j - increment];
j = j - increment;
}
A[j] = temp;
}
if (increment == 2)
increment = 1;
else
increment = (int) (increment / 2.2);
}
}
 
Merge Sort
 
In computer science, merge sort or mergesort is an O(n log n) comparison-based sorting algorithm. In most implementations it is stable, meaning that it preserves the input order of equal elements in the sorted output. It is an example of the divide and conquer algorithmic paradigm.
 
Example:
function mergesort(m)
var list left, right, result
if length(m) ≤ 1
return m
var middle = length(m) / 2
for each x in m up to middle
add x to left
for each x in m after middle
add x to right
left = mergesort(left)
right = mergesort(right)
result = merge(left, right)
return result

Quick Sort
 
Quicksort is a well-known sorting algorithm developed by C. A. R. Hoare that, on average, makes O(nlogn) (big O notation) comparisons to sort n items. However, in the worst case, it makes Θ(n2) comparisons. Typically, quicksort is significantly faster in practice than other Θ(nlogn) algorithms, because its inner loop can be efficiently implemented on most architectures, and in most real-world data it is possible to make design choices which minimize the probability of requiring quadratic time. Quicksort is a comparison sort and, in efficient implementations, is not a stable sort.
 
Example:
function quicksort(array)
     var list less, greater
     if length(array) ≤ 1 
         return array 
     select and remove a pivot value pivot from array
     for each x in array
         if x ≤ pivot then append x to less
         else append x to greater
     return concatenate(quicksort(less), pivot, quicksort(greater))

Hope this will help you,
Don't forget to LIFE SAVER RATE to my answer plz.

Ziaxp (MIT)




By reading or posting messages on these forums, you are agreeing to the Answer Board's Terms of Service and Conduct (TSC).


About Cramster | Terms of Use | Privacy Policy | Contact Us | Press Room | Site Map | Support | Anti-Cheating Policy

Cramster.com is not affiliated with any publisher. Book covers, title and author names appear for reference only.
Copyright © 2008 Cramster, Inc.