1.Create list of numbers(ex. 5, 4, 1, 6)
2.If 5(first number on the left) is smallest number, change none. Then go to step 8.
3.But if 5 isn't smallest number in list, then compare 5 with 4(second number) for find smaller number
4.5 is larger than 4, then change position 5 with 4(4, 5, 1, 6).
5.And compare 4 with 1(third number). 1 is larger than 4. So change 4 with 1(1, 5, 4, 6)
6.Then compare 1 with 6(fourth number). 1 is smaller than 6, then change none.
7.We compare all of numbers then knew 1 is smallest number.
8.If we found smallest number, left that number and find next smallest number.
8.Repeat step 2~8.
9.It end when we repeat step 2~8 n(numbers in the list of numbers)-1 times.
This is work for any numbers because it just compare n number with the next number in the list then repeats it to find the order of numbers. And this is in finite time because we need to repeats just n-1 times.
Sunday, September 26, 2010
Sunday, September 19, 2010
Write an algorithm to search for a number in a list of given numbers
Search for a number in a list [a1, a2, a3, an] of given number [k]
1.Compare k with a1(first number in a list)
2.If k and number in a list is equal, do the step 4, else number in a list is not equal as k, do the step 3.
3.If k and a1(number that pick in a list) isn't equal, repeat step 1~2 with next number in a list.
4.If return "YES", display that number.
This algorithm is correct, because if we compare given number with each number in a list(al, a2, a3, an), we can find number that equal with given number. Also, this is finiteness because it should find correct number with repeat least n times(this case, n times is 4 times).
[I am living in US almost 6 years, but I'm still not very well English-speaker and take hard time with grammer, therefore, maybe my algorithm with explain for correctness and finiteness are hard to read and understand. But believe I got this algorithm]
1.Compare k with a1(first number in a list)
2.If k and number in a list is equal, do the step 4, else number in a list is not equal as k, do the step 3.
3.If k and a1(number that pick in a list) isn't equal, repeat step 1~2 with next number in a list.
4.If return "YES", display that number.
This algorithm is correct, because if we compare given number with each number in a list(al, a2, a3, an), we can find number that equal with given number. Also, this is finiteness because it should find correct number with repeat least n times(this case, n times is 4 times).
[I am living in US almost 6 years, but I'm still not very well English-speaker and take hard time with grammer, therefore, maybe my algorithm with explain for correctness and finiteness are hard to read and understand. But believe I got this algorithm]
Sunday, September 12, 2010
ASU101 HOMEWORK-MAX
public class ASU101MAX{
public static void main(String args[]){
int[] nums={4,-1,-2,6,0,1,-5,3,7};
Arrays.sort(nums);
System.out.println("Minimum = " + nums[0]);
System.out.println("Maximum = " + nums[nums.length-1]);
}
}
---------------------------------------------------------------------------------------------------
I never built any programming before, and I this is my best right now.
public static void main(String args[]){
int[] nums={4,-1,-2,6,0,1,-5,3,7};
Arrays.sort(nums);
System.out.println("Minimum = " + nums[0]);
System.out.println("Maximum = " + nums[nums.length-1]);
}
}
---------------------------------------------------------------------------------------------------
I never built any programming before, and I this is my best right now.
Wednesday, September 8, 2010
Subscribe to:
Posts (Atom)