Algorithm series-----Matrix (eight)-------------matrix element ordering __ algorithm

Source: Internet
Author: User
Tags arrays sort

In fact, this content is to sort the column vector or the line vector, the essence is to do the sort of one-dimensional array

To be blunt is dimension processing:


To sort a one-dimensional array with bubbling method:

	Bubble method An array of elements from small to large sort
	/**
	 * Bubbles the elements in the array from small to large sort
	 * 
	 * @param args
	 *            parameter B floating-point (double) arrays
	 * @return The return value is a floating-point binary array
	 *
	/public static double[] Mintomax (double[] b) {
		Double temp = 0;
		for (int i = 0, i < b.length-1; i++) {for
			(int j = 0; J < B.length-1; J + +) {
				if (b[j + 1] < b[j]) {
					temp = b[j];
					B[J] = b[j + 1];
					B[j + 1] = temp;}}
		}
		return b;
	}

Test results:

Test Data
--------------------------------
	5.0	6.0	9.0	8.0
One-dimensional array sorting
-------------- ------------------
	5.0	6.0	8.0	9.0

	Bubbles the elements in an array from a large to a small sort
	/**
	 * Bubbles the elements in an array from large to small sort
	 * 
	 * @param args
	 *            parameter B floating-point (double) array
	 * @return The return value is a floating-point binary array
	 *
	/public static double[] Maxtomin (double[] b) {
		Double temp = 0;
		for (int i = 0, i < b.length-1; i++) {for
			(int j = 0; J < B.length-1; J + +) {
				if (b[j + 1] > B[j]) {
					temp = b[j];
					B[J] = b[j + 1];
					B[j + 1] = temp;}}
		}
		return b;
	}

Test results:

Test Data
--------------------------------
	5.0	6.0	9.0	8.0
One-dimensional array sorting
-------------- ------------------
	9.0	8.0	6.0	5.0

	Bubble method sorts the elements in an array from large to small, and records the position of the elements in the arrays after sorting
	/**
	 * Bubble method The elements in a logarithmic group are sorted from large to small
	 * 
	 * @param args
	 *            A one-dimensional array of parameter B floating-point (double)
	 * @return return value A is a two-dimensional array of floating-point (double), a new sort of the first record in the period, the second column records the original location */public
	static double[][] MaxToMin1 (double[] b) {
		double[][] a = new double[2][b.length];
		for (int i = 0; i < a[0].length; i++) {
			a[0][i] = b[i];
			A[1][i] = i;
		}
		Double temp = 0;
		Double location = 0;
		for (int i = 0, i < a[1].length-1; i++) {for
			(int j = 0; J < A[1].length-1; J + +) {
				if (a[0][j + 1] &G T A[0][j]) {
					temp = a[1][j];
					A[1][J] = a[1][j + 1];
					A[1][j + 1] = temp;
					location = A[0][j];
					A[0][J] = a[0][j + 1];
					A[0][j + 1] = location;

				}
			}
		}
		return A;
	}

Test results:

Test Data
--------------------------------
	5.0	6.0	9.0	8.0
One-dimensional array sorting
-------------- ------------------
	9.0	8.0	6.0	5.0
	2.0	3.0	1.0	0.0

Then we need to go to the matrix, just sort the row or column vectors, and then compare the size to the position of the value of each row a[i].

Matlab has a special method, but I have not learned the software. Interested people can go and see.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.