Make both sorting algorithms put the values in descending order. Then create drive class to test the two algorithms - drjava

I am extremely confused on how to reverse these sorting methods. Any help would be appreciated.
I have looked up and tried researching but I could not find anything to do with this type of comparable list.
public class Sorting
{
public static void selectionSort(Comparable[] list)
{
int min;
Comparable temp;
for (int index = 0; index < list.length-1; index++)
{
min = index;
for (int scan = index+1; scan < list.length; scan++)
if (list[scan].compareTo(list[min]) < 0)
min = scan;
temp = list[min];
list[min] = list[index];
list[index] = temp;
}
}
public static void insertionSort(Comparable[] list)
{
for (int index = 1; index < list.length; index++)
{
Comparable key = list[index];
int position = index;
while (position > 0 && key.compareTo(list[position-1]) < 0)
{
list[position] = list[position-1];
position--;
}
list[position] = key;
}
}
}

I think if you change:
if (list[scan].compareTo(list[min]) < 0)
to
if (list[scan].compareTo(list[min]) > 0)
it will sort in reverse order.
Here is the api
int compareTo(T o)
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Related

How to add JSpinner output into array and check for duplicate

I'm working on a code which has a JSpinner, which gives an output and moves it to int = n, I want to save every JSpinner output and add it into an array to check for duplicates and once found the JPanel should close itself or say you lost.
Scanner s = new Scanner(System.in);
int n = (Integer) spinner.getValue();
if (isPrime(n)) {
Input.setText(n + " is a prime number");
score++;
Highscore.setText("Score: " + score);
int[] array = new int[4];
for(int i=0; i<4;i++)
{
array[i]= (Integer) spinner.getValue();
}
for (int i=0; i < 4;i++)
{
System.out.println(array[i]);
}
Spinner output into array but it fills the whole array with one number example: [3,3,3,3].
private <T> boolean duplicate(T... array)
{
for (int i = 0; i < array.length; i++)
{
for (int j = i + 1; j < array.length; j++)
{
if (array[i] != null && array[i].equals(array[j])) {
return true;
dispose();
}
}
}
return false;
}
Duplicate check
I tried adding the user input from the JSpinner into an array and from there to check for duplicates.
Searched Online but could'nt find anything.
This if my first post so if you need anything more you can tell me.

How do I call functions within this code? Im using the visual studio software. keep getting errors that array1 and 2 are undefined

enter image description here
Sorry im new to coding . I have searched up possible solutions for this on here but they didnt work. Im also confused on why some code appears grey compared to the rest.
https://1drv.ms/w/s!Ag8vVFKVPyOg6HeYLehGjQKdvl_3?e=QHY6t9
#include <stdio.h>
// initialised variables
int i = 0;
int count = 0;
void displayfunction(void);
int month = 0;
void highervalues(float array1[12], float array2[12]);
void highervalues(float array1[12], float array2[12]) {
for (i = 12; i > 0; i--) {
if (array2[i] > array1[i]) {
count = count + 1;
}
}
}
//Reading values for array1 and array2
void displayfunction(void) {
highervalues(array1[12] , array2[12]);
for (i = 0; i < 12; i++)
{
month = month + 1; // month increases by 1 after each input
printf_s("enter values for year 1 month %d", month);
scanf_s("%f", &array1[i]);
}
for (month = 12; month > 0; month--) {
}
for (i = 0; i < 12; i++) {
month = month + 1;
printf_s("enter values for year 2 month %d", month);
scanf_s("%f", &array2[i]);
}
}
/*comapring 2 arrays and increasing count value if there are any value in array2
are greater than array1*/
int main() {
displayfunction();
int array1[12];
int array2[12];
}
You have a fundamental misunderstanding of variable scope
int main() {
displayfunction();
int array1[12];
int array2[12];
}
Those arrays are only available in main. If you want other functions to operate on them you have to pass them as paramters to those functions. Plus they dont exist at the point where you try to call displayFunction
So change
void displayfunction(void)
to
void displayfunction(float array1[12], float array2[12])
then in main do
int main() {
int a1[12];
int a2[12];
displayfunction(a1, a2);
}
Note that I have changed the names here just to emphasise that its the not the fact that the names are the same thats important.

Big O runtime - indexOf LinkedList/ ArrayList

I have a question considering Big O runtime and the indexOf method within LinkedList, and ArrayList. How can I come up with a Big O runtime assumption and how would it be different in a Linked List as opposed to an Array List?
LinkedList indexOf()
public int indexOf(Object value)
{
int results = -1;
boolean done = false;
Node<E> ref = head.next;
for(int i = 0; i < size && !done; i++)
{
if(value.equals(ref.value))
{
results = i;
done = true;
}
ref = ref.next;
}
return results;
}
ArrayList indexOf()
if (o == null) {
for (int i = 0; i < size; i++)
if (Values[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(Values[i]))
return i;
}
return -1;
I apologize if this is a trivial question to some but I am going to need to understand how to come up with a Big O runtime of a method.
In both these implementations you have nothing better to do than go over the list one element at a time and compare it to the value you're looking for. At the worst case, you'd be going through the entire list, giving a complexity of O(n).

Platform::Collections::Vector sorting

In Metro Style apps sometimes we use Platform::Collections::Vector to hold elements used in a ListView.
How to sort a Platform::Collections::Vector?
I'm aware there are plenty of structures in std that can be sorted but I was wondering if there was some method for Platform::Collections::Vector other than writing your own sort function.
Actually something like the below should also work:
auto vec = ref new Platform::Collections::Vector<T^>();
std::sort(begin(vec), end(vec));
I didn't find any suitable answer so I used this workaround.
It's a simple quicksort over a Platform::Collections::Vector
void swap (Platform::Collections::Vector<T^>^ vec, int pos1, int pos2)
{
T^ tmp = vec->GetAt(pos1);
vec->SetAt(pos1, vec->GetAt(pos2));
vec->SetAt(pos2,tmp);
}
int compare (T^ c1, T^ c2)
{
int c = wcscmp (c1->Title->Data(),c2->Title->Data());
return -c;
}
int PartitionVec (int left, int right,
Platform::Collections::Vector<T^>^ vec)
{
int i,j;
i = left;
for (int j = left + 1; j <= right; ++j)
{
if (compare (vec->GetAt(j),vec->GetAt(left)) > 0)
{
++i;
swap (vec,i,j);
}
}
swap (vec,left,i);
return i;
}
void QuickSortVec (Platform::Collections::Vector<T^>^ vec,
int start, int end)
{
if (end > start)
{
int pivot_point;
pivot_point = PartitionVec (start, end, vec);
QuickSortVec (vec,start,pivot_point - 1);
QuickSortVec (vec, pivot_point + 1, end);
}
}

implement feedback in lucene

Can someone give me hints to apply pseudo-feedback in lucene. I can not find much help on google. I am using Similarity classes.
Is there any class in lucene which I can extend to implement feedback ?
thanks.
Assuming you are referring to this relevance feedback method, Once you have the TopDocs for your original query, iterate through how ever many (let's say we'll want the top 25 terms for the top 25 docs of the original query) records you desire, and call IndexReader.getTermVectors(int), which will grab the information you need. Iterate through each. while storing the term frequencies in a hash map would be the implementation the immediately occurs to me.
Something like:
//Get the original results
TopDocs docs = indexsearcher.search(query,25);
HashMap<String,ScorePair> map = new HashMap<String,ScorePair>();
for (int i = 0; i < docs.scoreDocs.length; i++) {
//Iterate fields for each result
FieldsEnum fields = indexreader.getTermVectors(docs.scoreDocs[i].doc).iterator();
String fieldname;
while (fieldname = fields.next()) {
//For each field, iterate it's terms
TermsEnum terms = fields.terms().iterator();
while (terms.next()) {
//and store it
putTermInMap(fieldname, terms.term(), terms.docFreq(), map);
}
}
}
List<ScorePair> byScore = new ArrayList<ScorePair>(map.values());
Collections.sort(byScore);
BooleanQuery bq = new BooleanQuery();
//Perhaps we want to give the original query a bit of a boost
query.setBoost(5);
bq.add(query,BooleanClause.Occur.SHOULD);
for (int i = 0; i < 25; i++) {
//Add all our found terms to the final query
ScorePair pair = byScore.get(i);
bq.add(new TermQuery(new Term(pair.field,pair.term)),BooleanClause.Occur.SHOULD);
}
}
//Say, we want to score based on tf/idf
void putTermInMap(String field, String term, int freq, Map<String,ScorePair> map) {
String key = field + ":" + term;
if (map.containsKey(key))
map.get(key).increment();
else
map.put(key,new ScorePair(freq,field,term));
}
private class ScorePair implements Comparable{
int count = 0;
double idf;
String field;
String term;
ScorePair(int docfreq, String field, String term) {
count++;
//Standard Lucene idf calculation. This is calculated once per field:term
idf = (1 + Math.log(indexreader.numDocs()/((double)docfreq + 1))) ^ 2;
this.field = field;
this.term = term;
}
void increment() { count++; }
double score() {
return Math.sqrt(count) * idf;
}
//Standard Lucene TF/IDF calculation, if I'm not mistaken about it.
int compareTo(ScorePair pair) {
if (this.score() < pair.score()) return -1;
else return 1;
}
}
(I make no claim that this is functional code, in it's current state.)